MySQL INSERT INTO不起作用

时间:2014-12-16 16:25:40

标签: php mysql forms insert

我的PHP / MySQL代码存在问题。

<meta charset="utf-8">
<style type="text/css">body {text-align: center;}</style>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<table border="0" align="center">
    <tr>
        <th>
            <textarea  name="comment" placeholder="Okomentuj túto stránku...(Najviac 500 znakov)" rows="10" cols="100" maxlength="5000"></textarea>
        </th>
    </tr>
    <tr>
        <th>
            <input type="submit" name="submit" value="Odoslať">
        </th>
    </tr>
</table>
</form>
<?php
$submit = $_POST["submit"];
$name = $_SESSION['user_name'];
$comment = $_POST["comment"];
$datetime = date("y-m-d H:i:s");

$conn = mysqli_connect("localhost", "Kubajsk0", "", "login");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if($submit){
    if($name&& $comment){
        $insert=mysqli_query($conn,"INSERT INTO serverlist_comments (name, carriage, location, datetime) VALUES ('$name','$comment','$location','$datetime')");
    }else {
    echo "<script>alert('Prosím Ťa vyplň všetky polia :D');</script>";
}
} else {
echo "Nepodarilo sa tvoj koment uložiť do databázi :(<br/>
      Skús to neskôr :D";
}
$show_name = "SELECT name FROM `serverlist_comments` WHERE location = $location ORDER BY datetime DESC";
$result_name = mysqli_query($conn,$show_name);
$show_carriage = "SELECT carriage FROM serverlist_comments WHERE location = $location ORDER BY datetime DESC";
$result_carriage = mysqli_query($conn,$show_carriage);
while($row_name = mysqli_fetch_assoc($result_name)){
while($row_carriage = mysqli_fetch_assoc($result_carriage)){
        print($row_name['name']);
        echo " napísal/a toto : ";
        print($row_carriage['carriage']);
        echo "<br/>";
}
}
?>

<form>的HTML部分工作正常,数据库中我SELECT的部分也正常工作。

但是插入数据库正常工作。

2 个答案:

答案 0 :(得分:1)

相反

$insert=mysqli_query($conn,"INSERT INTO serverlist_comments (name, carriage, location, datetime) VALUES ('$name','$comment','$location','$datetime')");

$arInsert = array(
    'name' => mysqli_real_escape_string($conn, $name), 
    'carriage' => mysqli_real_escape_string($conn, $comment), 
    'location' => mysqli_real_escape_string($conn, $location), 
    'datetime' => mysqli_real_escape_string($conn, $datetime)   
);
$sql = "INSERT INTO serverlist_comments (".implode(',', array_keys($arInsert)).") VALUES ('".implode("','", $arInsert)."')"; 
$insert = mysqli_query($conn, $sql);

echo $sql;
if(!$insert)
    die(mysqli_error($conn));

输出是什么。

答案 1 :(得分:1)

  

试试这个

<?php
$submit = $_POST["submit"];
$name = $_SESSION['user_name'];
$comment = $_POST["comment"];
$location = //some value
$datetime = date("y-m-d H:i:s");

$conn = mysqli_connect("localhost", "Kubajsk0", "", "login");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST["submit"])){
    if($name && $comment){
        $insert=mysqli_query($conn,"INSERT INTO serverlist_comments (name, carriage, location, datetime) 
        VALUES ('".$name."','".$comment."','".$location."','".$datetime."')");
    }else {
    echo "<script>alert('Prosím Ťa vyplň všetky polia :D');</script>";
}
} else {
echo "Nepodarilo sa tvoj koment uložiť do databázi :(<br/>
      Skús to neskôr :D";
}
$show_name = "SELECT name,carriage FROM serverlist_comments WHERE location = '".$location."' ORDER BY datetime DESC";
$result_name = mysqli_query($conn,$show_name);
while($row_name = mysqli_fetch_array($result_name)){
    echo $row_name['name'];
    echo " napísal/a toto : ";
    echo $row_name['carriage'];
    echo "<br/>";
}
?>