gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:10:39 25 4
gpt4 key购买 nike

我正在尝试向 MySQL 表添加新行。它正在向我读取错误 Could not input data: Column count does not 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 列并尝试添加 3 列时您期望发生什么

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

谷歌搜索该错误会对您有所帮助。它准确地告诉你错在哪里。

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

我希望您也知道 $job_pos_sort 只是一个字符串,并且想要计算任何内容,尤其是在字符串中添加 1(也可能会出现错误)

并且,您最好切换到有关 mysql 的现代 DB API 之一 - mysqli 或 PDO。

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

关于PHP 表单不允许我向 MySQL 数据库添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com