我正在从事与Craigslist类似的项目,我想在提交表单后将日期导入到列“ CurrentDate”中。我想修复我的代码,以确保有两个错误。
由于网站仍在运行中,我没有做太多尝试。
if(isset($_POST['submit'])) { // Fetching variables of the form which travels in URL
$itemname = $_POST['itemname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
if($name !='' || $email !='') {
$query = mysql_query("insert into posts(itemname, category, subcategory, price) VALUES (NOW()'$itemname', '$category', '$subcategory', '$price')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else {
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
我希望在该字段中导出当前日期。
SQL序列:
答案 0 :(得分:-1)
您错过了一个逗号,并且未命名您要插入的列:
$itemname = $_POST['itemname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
if($name !=''||$email !=''){
//Add the name of the column here, and put a comma in the values list
$query = mysql_query("insert into posts(CurrentDate, itemname, category, subcategory, price) VALUES (NOW(),'$itemname', '$category', '$subcategory', '$price')");
//You are also telling the user the query ran successfully, without actually checking the value of $query to confirm if that is so.
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
然后:请注意收到的关于在生产服务器上运行此命令的警告。了解有关SQL注入和参数化查询的信息。在5.6之后的任何版本的PHP上,这也将不起作用,该版本也已经停产,并且不再维护安全性修复程序。
所以:问题得到了回答,但是一些学习提示供您继续。
答案 1 :(得分:-1)
$itemname = $_POST['itemname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
$cur_date = date('Y-m-d');
if($name !=''||$email !=''){
$query = mysql_query("insert into posts(current_date,itemname, category, subcategory, price) VALUES ($cur_date,'$itemname', '$category', '$subcategory', '$price')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
这是另一种方法,它将仅以YYYY-MM-DD格式存储当前日期,而不存储时间。 如果您还想存储时间,则在SQL查询中使用now()代替$ cur_date或 使用$ cur_date = date('Y-m-d H:i:s');