无法在php中使用mysql查询插入数据

时间:2014-07-16 11:35:38

标签: php mysql database insert workbench

我试图使用表单将一些数据插入到我的数据库中。在填写表格后,我正在使用方法=" 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 workbench在我的数据库中传递一些数据,但没有使用php代码。你能帮我吗?提前谢谢!

4 个答案:

答案 0 :(得分: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);
?> 

答案 1 :(得分:0)

尝试删除&#39;;&#39;从你查询。

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

答案 2 :(得分:0)

<?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);
?> 

答案 3 :(得分:0)

您的示例不是推荐的插入数据的方法(您应该真正使用mysqli->prepare来保证安全)。但是,要解决你在这里的问题:

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