我尝试将一些来自post值的值插入到我的db中,并且它不起作用.. 从1个帖子中只尝试了1个值并且它可以工作,而不是使用多个帖子。 没有错误,没有:(
代码:
$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
$new_id = ++$largestNumber;
$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){
$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
$insert = ("INSERT INTO topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'") or die(mysql_error());
$run = mysql_query($insert);
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}
答案 0 :(得分:3)
试试这个并发布你得到的内容:
$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
$new_id = ++$largestNumber;
$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){
$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
$insert = "INSERT INTO topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'";
echo $insert;
$run = mysql_query($insert) or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}
编辑:
如果你想插入一些数据,那你为什么把where子句放在那里?
EDIT2:
$update = "UPDATE topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'";
$run = mysql_query($update) or die(mysql_error());
EDIT3:
$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
$new_id = ++$largestNumber;
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){
$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
mysql_query("INSERT INTO topic SET id = '".$new_id."', nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."'") or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}
答案 1 :(得分:1)
看起来你正在编写代码来增加数据库中行的ID,当你可以让MySQL自己处理它时。如果在id字段上设置AUTO INCREMENT为true,那么您需要的是以下内容;
// Validate your POST inputs
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){
$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
$insert = "INSERT INTO topic (nome, desc, link) VALUES ('".$nome."', '".$desc."', '".$link."')";
echo $insert;
$run = mysql_query($insert) or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}
如果您将id字段设置为AUTO INCREMENT,则id会自动将其自身设置为列表中的下一个数字
答案 2 :(得分:0)
试试这个:
$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
$new_id = ++$largestNumber;
$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");
$newnumberid = mysql_query("SELECT id FROM topic WHERE id = '".$new_id."'");
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){
$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
$insert = "UPDATE topic(nome,desc,link) SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumberid."'";
echo $insert;
$run = mysql_query($insert) or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}