gpt4 book ai didi

php - 数据不会发布到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 06:40:35 25 4
gpt4 key购买 nike

这是我遇到的错误:

SQL QUERY: INSERT INTO portfolio  (portImg,portTitle,portDesc,portCat,portSkill,portDate) VALUES (IMAGE, TITLE, sadasdasd, CAT, SKILL, 2014-02-15 08:53:10)
Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
sadasdasd, CAT, SKILL, 2014-02-15 08:53:10)' at line 1

这是我的 PHP 代码:

<?php

//if form has been submitted process it
if(isset($_POST['submit'])){

$portImg =$_POST['portImg'];
$portTitle =$_POST['portTitle'];
$desc=$_POST['portDesc'];
$portDesc = trim($desc);
$portCat=$_POST['portCat'];
$portSkill=$_POST['portSkill'];
$portDate=date('Y-m-d H:i:s');

//very basic validation
if($portImg ==''){
$error[] = 'Please enter the title.';
}
if($portTitle ==''){
$error[] = 'Please enter the title.';
}

if($portDesc ==''){
$error[] = 'Please enter the description.';
}

if($portCat ==''){
$error[] = 'Please enter the content.';
}

if($portSkill ==''){
$error[] = 'Please enter the content.';
}

if(!isset($error)){

$query="INSERT INTO portfolio (portImg,portTitle,portDesc,portCat,portSkill,portDate) VALUES ($portImg, $portTitle, $portDesc, $portCat, $portSkill, $portDate)";


echo "SQL QUERY: ".$query."<br />";


if (!mysql_query($query))
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
}

//redirect to index page
header('Location: index.php?action=added');
exit;

}

//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p class="error">'.$error.'</p>';
}
}
?>

这似乎是由 Textarea(描述)引起的问题。好像是前后加空格。有什么想法吗?

最佳答案

对于字符串字段,您必须在插入过程中用 ' 将您的值括起来...例如:

SQL QUERY: INSERT INTO portfolio (portImg,portTitle,portDesc,portCat,portSkill,portDate)
VALUES ('IMAGE', 'TITLE', 'sadasdasd', 'CAT', 'SKILL', '2014-02-15 08:53:10')

所以你的php应该是

INSERT INTO portfolio (portImg,portTitle,portDesc,portCat,portSkill,portDate)
VALUES ('$portImg', '$portTitle', '$portDesc', '$portCat', '$portSkill', '$portDate')

关于php - 数据不会发布到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799183/

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