dtgu21994537 2012-06-21 15:13
浏览 68

为什么我的代码不会在我的数据库中插入值?

I'm busy with an assignment of a course and I can't find out what the problem is. The only thing I know is that it's in the insert part. The connection is correct. As I'll show the variables, you can see the words which where put in the input fields. But they are still not in the database.

I used this code before and it worked but I don't see why it doesn't work at this moment. Can anyone please help me? Here below is the code I have.

Thank you for trying to help me!!

<?php

// Open connectie naar de database
$link =  mysql_connect('localhost', 'root', 'root'); // maakt de connectie met de databases (mamp/wamp)
if (!$link) {
    die('Geen connectie ' . mysql_error()); // verbreekt de verbinding en laat de error zien
}

$db_selected = mysql_select_db('voorbeeld', $link); // maakt connectie het database "voorbeeld"
if (!$db_selected) {
    die ('Kan database niet selecteren : ' . mysql_error());// verbreekt de verbinding en laat de error zien
}

if ($_POST['submit'] == 'Verzenden') {

  $naam = $_POST['naam'];
  $boodschap = $_POST['boodschap'];

  $datum = date("y-m-d");  

  // Bericht opslaan
  $query2 = "INSERT INTO gastenboek (ID, naam, boodschap, datum) 
        VALUES(NULL, '$naam', '$boodschap', '$datum')";
  $result = mysql_query($query2);

  if (mysql_affected_rows() == 1) {
     $success_msg = '<P>Uw bericht is geplaatst.</P>';

  } else {
    error_log(mysql_error());
    $success_msg = '<P>Helaas, er ging iets mis.</P>';
  }

}



$thispage = $_SERVER['PHP_SELF'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


<?php
$form_page = <<< EOFORMPAGE
</head>

<body>

<H1>Uw bericht</H1>

$datum
$boodschap
$success_msg

<form method="POST" action="$thispage">

Uw naam:<br/>
<input type="text" SIZE="40" name="naam" value="$naam"><br/><br/>
Uw bericht:<br/>
<textarea name="boodschap" rows=10 cols=50>$boodschap</textarea><br/><br/>
<input type="submit" name="submit" value="Verzenden">
</form>


<a href="gastenboek.php"><input type="button" name="gastenboek" value="Gastenboek"></a>

</body>
</html>

EOFORMPAGE;
echo $form_page;
?>
  • 写回答

2条回答 默认 最新

  • douyi5822 2012-06-21 15:35
    关注

    (From my comment)

    It looks like you're trying to insert NULL into an ID field. If that column uses a default value or a seed/incrementing value, you can just omit it:

    $query2 = "INSERT INTO gastenboek (naam, boodschap, datum) 
               VALUES('$naam', '$boodschap', '$datum')";
    

    Also, even though it's just a homework assignment, it's a good idea to get familiar with security vulnerabilities exposed by not sanitizing user input. I'd get familiar with this:

    http://php.net/manual/en/security.database.sql-injection.php

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么