PHP表单不允许我向MySQL数据库添加新行

时间:2014-02-10 23:15:20

标签: php mysql sql

我正在尝试将新行添加到MySQL表中。它正在读给我错误Could not enter data: Column count doesn't match value count at row 1。到目前为止,我正在使用代码

if(! get_magic_quotes_gpc() )
{
   $job_pos = addslashes ($_POST['job_pos']);
}
else
{
   $job_pos = $_POST['job_pos'];
}
$job_pos_sort = "SELECT LAST(job_pos_sort) FROM careers;" + 1;

$sql = "INSERT INTO careers ".
       "(job_pos, job_pos_sort) ".
       "VALUES('$job_pos', '$job_pos_sort', NOW())";

在表格中插入新行。

以下是我的整个页面代码,我的页面可以在http://thetotempole.ca/phptester/upanddowntest.php看到:

<?php
// connect to db
$conn = mysql_connect("xxxx","x","x","x") or die(mysql_error());
$db = mysql_select_db('x',$conn) or die(mysql_error());

// if an arrow link was clicked...
if ($_GET['dir'] && $_GET['id']) {
   // make GET vars easier to handle
   $dir = $_GET['dir'];
   // cast as int and couple with switch for sql injection prevention for $id
   $id = (int) $_GET['id'];
   // decide what row we're swapping based on $dir
   switch ($dir) {
      // if we're going up, swap is 1 less than id
      case 'up': 
         // make sure that there's a row above to swap
         $swap = ($id > 1)? $id-- : 1;
         break;
      // if we're going down, swap is 1 more than id
      case 'down':
         // find out what the highest row is
         $sql = "SELECT count(*) FROM careers";
         $result = mysql_query($sql, $conn) or die(mysql_error());
         $r = mysql_fetch_row($result);
         $max = $r[0];
         // make sure that there's a row below to swap with
         $swap = ($id < $max)? $id++ : $max;
         break;
      // default value (sql injection prevention for $dir)
      default:
         $swap = $id;
   } // end switch $dir
   // swap the rows. Basic idea is to make $id=$swap and $swap=$id 
   $sql = "UPDATE careers SET job_pos_sort = CASE job_pos_sort WHEN $id THEN $swap WHEN $swap THEN $id END WHERE job_pos_sort IN ($id, $swap)";
   $result = mysql_query($sql, $conn) or die(mysql_error());
} // end if GET  

// set a result order with a default (sql infection prevention for $sortby)
$sortby = ($_GET['sortby'] == 'job_pos')? $_GET['sortby'] : 'job_pos_sort';
// pull the info from the table
$sql = "SELECT job_pos_sort, job_pos FROM careers ORDER BY $sortby";
$result = mysql_query($sql, $conn) or die(mysql_error());

// display table
echo "<table border = '1'>";
echo "<tr>";
// make column names links, passing sortby
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=job_pos_sort'>job_pos_sort</a></td>";
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=job_pos'>job_pos</a></td>";
echo "</tr>";
// display data 1 row at a time
while ($r = mysql_fetch_assoc($result)) {
   echo "<tr>";
   // make the links to change custom order, passing direction and the custom sort id
   echo "<td align = 'center'><a href='{$_SERVER['PHP_SELF']}?dir=up&id={$r['job_pos_sort']}'>/\</a> ";
   echo "<a href='{$_SERVER['PHP_SELF']}?dir=down&id={$r['job_pos_sort']}'>\/</a></td>";
   echo "<td>{$r['job_pos']}</td>";
   echo "</tr>";
} // end while $r
echo "</table>";
// end display table
?>

<html>
<head>
<title>Manage Careers</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'x';
$dbuser = 'xx';
$dbpass = 'xx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $job_pos = addslashes ($_POST['job_pos']);
}
else
{
   $job_pos = $_POST['job_pos'];
}
$job_pos_sort = "SELECT LAST(job_pos_sort) FROM careers;" + 1;

$sql = "INSERT INTO careers ".
       "(job_pos, job_pos_sort) ".
       "VALUES('$job_pos', '$job_pos_sort', NOW())";
mysql_select_db('x');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Job Position</td>
<td><input name="job_pos" type="text" id="job_pos"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Job Position">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

感谢任何帮助。

此致

凯尔

2 个答案:

答案 0 :(得分:2)

我不知道在指定2列时会发生什么,并尝试添加3

   "INSERT INTO careers ".
   "(job_pos, job_pos_sort) ".
   "VALUES('$job_pos', '$job_pos_sort', NOW())

Google搜索错误会对您有所帮助。它确切地说明了你的错误。

job_posjob_pos_sort,但值 - job_posjob_post_sortNOW()。您可能必须指定最后一列,它似乎是一个日期时间

我希望你也知道$ job_pos_sort只是一个字符串,并且想要评估任何东西,特别是在字符串中加1(它也可能会出错)

并且,您最好切换到关于mysql - mysqli或PDO的现代数据库API之一。

http://www.php.net/manual/en/mysqlinfo.api.choosing.php

答案 1 :(得分:0)

你应该运行这个:

SHOW COLUMNS FROM careers;

那么也许我们知道这里缺少xxxxxx字段的名称 它必须是某种约会。

$sql = "INSERT INTO careers ".
       "(job_pos, job_pos_sort, xxxxxx) ".
       "VALUES('$job_pos', '$job_pos_sort', NOW())";

或者只是试试这个:

$sql = "INSERT INTO careers ".
       "(job_pos, job_pos_sort) ".
       "VALUES('$job_pos', '$job_pos_sort')";

这应该有效