如何使用php向mysql数据库添加新记录

时间:2014-03-08 07:20:27

标签: php mysql

我正在尝试连接到XAMPP MySQL数据库以添加新记录。我相信我做的一切都正确,但由于某种原因,我的数据库表中没有添加新行。任何人都可以查看代码并告诉我什么是错的?提前谢谢。

<?php
error_reporting(0);
$connect = mysql_connect('localhost','root','');
mysql_select_db("projects", $connect);
$button = $_POST['button'];
$username = $_POST['username'];
$seatingplan = $_POST['seatingplan'];
$numberofseats = $_POST['numberofseats'];
$date= $_POST['date'];
$branch = $_POST['branch'];

if ($button)
{
if($username && $seatingplan && $numberofseats && $date &&$branch)
{
mysql_query("INSERT INTO reservations(Username, Seating plan, Number of seats, Date, Branch)VALUES('$username','$seatingplan','$numberofseats,'$date','$branch')");
}
else
echo"You did not fill in the fields";
}
?>
<html>
<link type="text/css" rel="stylesheet" href="style codecademy.css">
<body background="http://hative.com/wp-content/uploads/2013/06/chinese-traditional-bamboo-background-template-4260.jpg">
 <h1 style="color:orange"> <b> <i> Reservation Form</i> </b> </h1>
<form action="reservation.php" method="POST">
<form>
<b style="color:orange; font-size:20px">Seating plan:<name="seatingplan"></b>
<select> Seating plan
  <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="13">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
  <option value="16">16</option>
  <option value="17">17</option>
  <option value="18">18</option>
  <option value="19">19</option>
  <option value="20">20</option>
</select>
<b style="color:orange; font-size:20px">Number of seats: <input type="text" name="numberofseats"></b>
</form>
 <img src="plan.jpg"alt="plan" width="450" height="350">
<form><b style="color:orange; font-size:20px"> Pick the date :<name="date"> </b>
<script type="text/javascript"
src="http://www.snaphost.com/jquery/Calendar.aspx"></script>
</form>
<form>
<b style="color:orange; font-size:20px">Restaurant branch:<name="branch"></b>
<select>Restaurant branch
  <option value="Saryarka Ave.">Saryarka Ave.</option>
  <option value="Zhubanov Str.">Zhubanov Str.</option>
</select>
</form>
<b style="color:orange; font-size:20px">Username:<input type="text" name="username"></b>
<input type="submit" name="button" value="Submit">
</form>
<script type="text/javascript">
window.onload = function(){
    var json = '{"servers":{"server1":1,"server2":2},"slots":{"slotOne":1,"slotDual":0.5,"slotTripple":0.7,"slotMonstro":2.857142857142857},"rents":{"week":7,"day":1,"oneYear":365,"forewer":null}}'; //this string is beckoming from the server
    data = JSON.parse(json);    
}
function recalc() {
    var server = document.getElementById('server').value;
    var slot = document.getElementById('slot').value;
    var rent = document.getElementById('rent').value;
    var formul9;
    try {
    formul9 = (data.servers[server]+data.slots[slot])*data.rents[rent];
    } catch (e) { }
    if(isNaN(formul9)) formul9 = +Infinity;
    formul9 = parseFloat(formul9).toFixed(2);
    document.getElementById('total').innerHTML = formul9.toString();
};

</script>



</body>
</html>

4 个答案:

答案 0 :(得分:1)

如果列名中有spaces,则需要使用字符“

封装列名称
 mysql_query("INSERT INTO reservations(`Username`, `Seating plan`, `Number of seats`, `Date`, `Branch`) VALUES('$username','$seatingplan','$numberofseats,'$date','$branch')");

答案 1 :(得分:1)

您必须记住db field_name这样的事情不应该有空格,其次您没有检查过该按钮,值是set还是最后一件事是使用concatenationquery中,或在代码下面替换

<?php
if(isset($button))
{
  if(isset($username && $seatingplan && $numberofseats && $date &&$branch))
  {
   mysql_query("INSERT INTO reservations (`Username`,`Seating plan`,`Number of seats`,`Date`,`Branch`)VALUES('".$username."','".$seatingplan."','".$numberofseats."','".$date."','".$branch."')");
}

答案 2 :(得分:0)

我认为查询错误试试这个

mysql_query("INSERT INTO reservations(`Username`, `Seating plan`, `Number of seats`, `Date`, `Branch`) VALUES('".$username."','".$seatingplan."','".$numberofseats."','".$date."','".$branch."')");

答案 3 :(得分:-1)

您无法在列名称中添加空格

错误

mysql_query("INSERT INTO reservations(Username, Seating plan, Number of seats, Date, Branch)VALUES('$username','$seatingplan','$numberofseats,'$date','$branch')");

在更正colunm名称后尝试以下代码。您可以使用_代替列名称中的空格。

<强> PHP

   <?php
        error_reporting(0);
        $connect = mysql_connect('localhost','root','');
        mysql_select_db("projects", $connect);
        $button = $_POST['button'];
        $username = $_POST['username'];
        $seatingplan = $_POST['seatingplan'];
        $numberofseats = $_POST['numberofseats'];
        $date= $_POST['date'];
        $branch = $_POST['branch'];

    if ($button)
    {
        if($username && $seatingplan && $numberofseats && $date &&$branch)
        {
            mysql_query("INSERT INTO reservations(Username, Seating_plan, Number_of_seats, Date, Branch)VALUES('$username','$seatingplan','$numberofseats,'$date','$branch')");
        }
    else
        echo"You did not fill in the fields";
    }
    ?>