dongtuota3633
2015-06-23 09:00
浏览 64
已采纳

使用php将数据输入mysql数据库

Customer will complete a form and enter a pathway where they will want the CSV to be exported to. The pathway is entered using the top section of the php (below):

    <form action="register.php" method="post">
        Enter file pathway where CSV will be saved: <input type="text" name="username" required="required"/> <br/>
        <input type="submit" value="Enter"/>
    </form>
</body>

I want to create a variable called pathway. At the moment I can get text entered into the correct row in the mysql database (I can get John printed in the database), but not the correct text that was entered into the form (i.e. $pathway). I want to create a variable because after saving the pathway in the database i also want to use it in an export.php.

I am assuming i also need something like this:

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $pathway = mysql_real_escape_string($_POST['pathway']); 
// but i can't seem to piece it altogether.


    <?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "first_db";
    $table_users = $row['pathway'];
    $pathway = "pathway";                                             

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO users (pathway)
    VALUES ('John')";

    if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>

图片转代码服务由CSDN问答提供 功能建议

客户将填写表格并输入他们希望将CSV导出到的路径。 使用php(下面)的顶部输入路径:

 &lt; form action =“register.php”method =“post”&gt;  
输入将保存CSV的文件路径:&lt; input type =“text”name =“username”required =“required”/&gt;  &lt; br /&gt; 
&lt; input type =“submit”value =“Enter”/&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
   
 \  n 

我想创建一个名为path的变量。 目前我可以将文本输入到mysql数据库中的正确行(我可以将John打印在数据库中),但不是输入到表单中的正确文本(即$途径)。 我想创建一个变量,因为在数据库中保存路径之后我还想在export.php中使用它。

我假设我也需要这样的东西:

  if($ _ SERVER [“REQUEST_METHOD”] ==“POST”){
 $ pathway = mysql_real_escape_string($ _ POST ['pathway']);  
 //但是我似乎无法将它完全分开。
 
 
&lt;?php 
 $ servername =“localhost”; 
 $ username =“”; 
 $ password =“”;  
 $ dbname =“first_db”; 
 $ table_users = $ row ['pathway']; 
 $ pathway =“pathway”;  
 
 //创建连接
 $ conn = mysqli_connect($ servername,$ username,$ password,$ dbname); 
 //检查连接
 if(!$ conn){
 die(“连接失败)  :“。mysqli_connect_error()); 
} 
 
 $ sql =”INSERT INTO users(途径)
 VALUES('John')“; 
 
 if(mysqli_query($ conn,$ sql))  {
 echo“新记录成功创建”; 
} else {
 echo“错误:”。  $ sql。  “&LT峰; br&gt;” 中 。  mysqli_error($ conn); 
} 
 
 mysqli_close($ conn); 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dpn4073 2015-06-23 09:15
    已采纳

    This shoud work, if not then check your usename and password...

    <?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "first_db";
    $pathway = $_POST['username'];  username - is the name of your input.                                             
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn)
    {
        die("Connection failed: " . mysqli_connect_error());
    }
    
    $sql = "INSERT INTO users (pathway)
        VALUES ('$pathway')";
    
    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    }
    else
    {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    
    mysqli_close($conn);
    ?>
    
    点赞 评论
  • doufusi2013 2015-06-23 09:11

    your DB username is Null

    Change $username = ""; to $username = "root";

    点赞 评论
  • dstobkpm908182 2015-06-23 09:12

    Your input field name is username

    change it to pathway for $_POST['pathway'] to work

    <form action="register.php" method="post">
        Enter file pathway where CSV will be saved: 
         <input type="text" name="pathway" required="required"/> <br/>
        <input type="submit" value="Enter"/>
    </form>
    
    点赞 评论
  • dongou4052 2015-06-23 09:13

    First of all, you've got 'username' as the name of the field using for type a pathway, so rename it to 'pathway'. I don't know if I understand you, but do you just want to read posted content? Try something like:

    $pathway = $_POST['pathway']
    

    I strongly recommend to use object-oriented style with

    $conn = new mysqli...
    

    and then

    mysqli->prepare(), mysqli->bind_param(), mysqli->execute()
    

    With this you won't have to deal with mysqli_real_escape_string etc.

    点赞 评论

相关推荐 更多相似问题