-3

我正在将表单发送到 php 页面,然后将其保存到 mysql 数据库中。代码似乎执行没有错误,但没有值保存到数据库中。

我是否错过了代码中的某些内容?

形式:

<form action="newcourse.php" method="post">
<input name="title" type="text" id="title" value="">
<br /><br />
  <input name="location" type="text" id="location" value="">
<br /><br />
<input name="datestart" type="text" id="datestart" value="">
<br /><br />
<input name="dateend" type="text" id="dateend" value=""><br /><br />
<select name="noofweeks" id="noofweeks">
  <option value="1" selected>1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
</select><br /><br />
<select name="wheelchair" id="wheelchair">
  <option value="yes">yes</option>
  <option value="no" selected>no</option>
</select>
<br /><br />
<input name="time" type="text" id="time" value=""><br /><br />
<input name="courseleader" type="text" id="courseleader" value=""><br /><br />
<input name="pack" type="hidden" id="pack" value="participants_pack.pdf">
<input name="price" type="text" id="price" value=""><br /><br />
<input name="group" type="text" id="group" value=""><br /><br />
<input name="numberofplaces" type="text" id="numberofplaces" value=""><br /><br />
<select name="bookings" id="bookings">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="0" selected>0</option>
</select>
<br /><br />
<br /><br />
<input name="submit" type="submit" value="Save"></p></form>

保存到mysql数据库:

<?php
$title=$_POST['title'];
$location=$_POST['location'];
$datestart=$_POST['datestart'];
$dateend=$_POST['dateend'];
$noofweeks=$_POST['noofweeks'];
$wheelchair=$_POST['wheelchair'];
$time=$_POST['time'];
$courseleader=$_POST['courseleader'];
$pack=$_POST['pack'];
$price=$_POST['price'];
$group=$_POST['group'];
$numberofplaces=$_POST['numberofplaces'];
$bookings=$_POST['bookings'];
//
require_once('connecttodatabase.php');
//
mysql_query("INSERT INTO courses (title, location, datestart, dateend, noofweeks, wheelchair, time, courseleader, pack, price, group, numberofplaces, bookings)  VALUES('$title', '$location', '$datestart', '$dateend', '$noofweeks', '$wheelchair', '$time', '$courseleader', '$pack', '$price', '$group', '$numberofplaces', '$bookings')");
mysql_close();
echo "Database updated!";
?>

connecttodatabase.php 只是连接到数据库并适用于其他脚本,所以我知道这不是问题。

4

3 回答 3

1

您必须从 POST 中获取这些变量,例如:

$title = $_POST['title'];

好的,您的编辑表明您正在这样做,如果我发现任何东西,我会更新它。

我建议查看 Bondye 的答案并使用:

mysql_query(...) or die(mysql_error());

因此您可以看到从 SQL 数据库返回的错误消息。

于 2012-10-01T14:43:08.713 回答
0

您也使用timeandgroup作为保留的 sql 关键字的列名,在列名周围使用反引号。我正在重写您的代码 lil ..此外,不要使用提交的名称作为提交,使用像any_name //Change anyname to whatever你这样的其他东西喜欢

<?php
require_once('connecttodatabase.php');

if(isset($_POST['any_name'])) {
$title=$_POST['title'];
$location=$_POST['location'];
$datestart=$_POST['datestart'];
$dateend=$_POST['dateend'];
$noofweeks=$_POST['noofweeks'];
$wheelchair=$_POST['wheelchair'];
$time=$_POST['time'];
$courseleader=$_POST['courseleader'];
$pack=$_POST['pack'];
$price=$_POST['price'];
$group=$_POST['group'];
$numberofplaces=$_POST['numberofplaces'];
$bookings=$_POST['bookings'];

   if(mysql_query("INSERT INTO courses (title, location, datestart, dateend, noofweeks, wheelchair, `time`, courseleader, pack, price, `group`, numberofplaces, bookings)  VALUES('$title', '$location', '$datestart', '$dateend', '$noofweeks', '$wheelchair', '$time', '$courseleader', '$pack', '$price', '$group', '$numberofplaces', '$bookings')")) {
     echo 'success';
      } else {
         echo 'Failed'.' '.mysql_error();
       }
    }
?>

PS 开始使用mysqli-参考

于 2012-10-01T14:55:15.047 回答
-1

尝试这个

<?php
$title = $_POST['title']
//and rest

mysql_query("INSERT INTO courses (title, location,....) VALUES('{$title}', '{$location}', '{$datestart}', '{$dateend}'.....')");

?>
于 2012-10-01T14:47:18.177 回答