2014-02-10 62 views
0

我想添加一個新行到一個MySQL表。它正在讀取我的錯誤Could not enter data: Column count doesn't match value count at row 1。到目前爲止,我正在使用代碼PHP表單不會讓我添加一個新行到MySQL數據庫

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> 

任何幫助表示讚賞。

問候,

凱爾西

+0

你爲什麼不使用的job_pos_sort AUTO_INCREMENT,並沒有什麼在MySQL LAST把它稱爲是MAX,另外作爲下面的答案是插入3列中的2列,並且$ job_post_sort將作爲字符串插入到數據庫中 – CodeBird

+0

我嘗試了自動增量,當試圖向上或向下移動一行時,它給了我一個錯誤。感謝Royal Bg,我確實設法解決了這個問題。 – Kelsey

回答

2

我不知道你期待指定2列時,並嘗試添加3

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

谷歌搜索的錯誤會幫助你發生什麼。它確切地告訴你錯在哪裏。

job_pos,job_pos_sort,但值 - job_pos,job_post_sortNOW()。您可能必須指定最後一列,這似乎是日期時間的一個

我希望您也知道$ job_pos_sort只是一個字符串,並且想要評估任何內容,特別是在將字符串加1(可能也出現錯誤)

而且,您最好切換到關於mysql-mysqli或PDO的現代DB API之一。

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

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')"; 

這應該工作

相關問題