簡體   English   中英

無法在php中使用mysql查詢插入數據

[英]Unable to insert data using mysql query in php

我正在嘗試使用表單將一些數據插入數據庫。 填寫表格后,我將使用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代碼傳遞數據。 你能幫我嗎? 提前致謝!

不建議使用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);
?> 

嘗試刪除“;” 從您查詢。

這應該是您最好的資源http://php.net/manual/en/function.mysql-query.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);
?> 

采樣不是插入數據的推薦方法(實際上應該使用mysqli->prepare以確保安全)。 但是,要解決這里的問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM