【问题标题】:Unable to insert data using mysql query in php无法在php中使用mysql查询插入数据
【发布时间】:2014-07-16 11:35:37
【问题描述】:

我正在尝试使用表单将一些数据插入到我的数据库中。填写表格后,我正在使用 method="POST" 获取数据。尽管我无法将表单中的数据插入数据库,但我没有语法错误。

这是一些php的代码:

<?php

    // Connects to your Database
    $con = mysql_connect("localhost", "root", "134711Kk", "eam3");

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    //This is the directory where images will be saved
    $target = "dokupload/";
    $target = $target . basename( $_FILES['photo']['name']);
    echo $target;
    echo "<br>";

    $name=$_POST['nameMember'];
    $bandMember=$_POST['bandMember'];


    if(!$con)
    {
        die("Connection Failed".mysql_error());     
    }

    echo "$name" . " " . "$bandMember" . "<br/>";

    $sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '$name', '$bandMember');";   

    $some = mysql_query($sql, $con);

    $request = mysql_query("SELECT * FROM eam3.Grammateia;", $con);

    while ($row = mysql_fetch_array($request))
    {   
        extract($row);
        echo "$id" . " " . "$idruma" . " " . "$tmhma" . "<br/>";
    }


    .
    .
    .

    mysql_close($con);
?> 

我设法通过 mysql 工作台在我的数据库中传递了一些数据,但没有使用 php 代码。你能帮我吗?提前致谢!

【问题讨论】:

  • 现在更改您的密码。并了解 PHP 的 mysql_ API 的弃用、sql 注入和prepared statements。

标签: php mysql database insert workbench


【解决方案1】:

使用 mysqli 代替 mysql,因为它已被弃用。

mysqli_query($con,$sql)而不是mysqli_query($sql,$con);

试试下面的代码

<?php

    // Connects to your Database
    $con = mysqli_connect("localhost", "root", "134711Kk", "eam3");

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    //This is the directory where images will be saved
    $target = "dokupload/";
    $target = $target . basename( $_FILES['photo']['name']);
    echo $target;
    echo "<br>";

    $name=$_POST['nameMember'];
    $bandMember=$_POST['bandMember'];


    if(!$con)
    {
        die("Connection Failed".mysqli_error());     
    }

    echo "$name" . " " . "$bandMember" . "<br/>";

    $sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '$name', '$bandMember');";   

    $some = mysqli_query($con,$sql);

    $request = mysqli_query($con,"SELECT * FROM eam3.Grammateia");

    while ($row = mysqli_fetch_array($request))
    {   
        extract($row);
        echo "$id" . " " . "$idruma" . " " . "$tmhma" . "<br/>";
    }
    mysqli_close($con);
?> 

【讨论】:

    【解决方案2】:

    尝试删除';'来自您的查询。

    这应该是你最好的资源http://php.net/manual/en/function.mysql-query.php

    【讨论】:

    • 为什么指向已弃用的 API?虽然,公平地说,我认为该链接至少表明它已被弃用。
    【解决方案3】:
    <?php
    
    // Connects to your Database
    $con = mysql_connect("localhost", "root", "134711Kk", "eam3");
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    //This is the directory where images will be saved
    $target = "dokupload/";
    $target = $target . basename( $_FILES['photo']['name']);
    echo $target;
    echo "<br>";
    
    $name=$_POST['nameMember'];
    $bandMember=$_POST['bandMember'];
    
    
    if(!$con)
    {
        die("Connection Failed".mysql_error());     
    }
    
    echo "$name" . " " . "$bandMember" . "<br/>";
    
    $sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '$name', '$bandMember')";   
    
    $some = mysql_query($sql, $con);
    
    $request = mysql_query("SELECT * FROM eam3.Grammateia;", $con);
    
    while ($row = mysql_fetch_array($request))
    {   
        extract($row);
        echo "$id" . " " . "$idruma" . " " . "$tmhma" . "<br/>";
    }
    
    .
    .
    .
    
    mysql_close($con);
    ?> 
    

    【讨论】:

      【解决方案4】:

      您的示例不是插入数据的推荐方法(为了安全起见,您确实应该使用mysqli-&gt;prepare)。但是,要修复这里的问题:

      $sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '" . $name . "', '" . $bandMember . "');";   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多