PHP MySQLi问题,预处理语句导致错误
我有一个问题,试图让php脚本为我的网站工作,我可以添加新的项目到我的网上商店 我遇到的代码片段如下:PHP MySQLi问题,预处理语句导致错误,php,mysql,mysqli,prepared-statement,Php,Mysql,Mysqli,Prepared Statement,我有一个问题,试图让php脚本为我的网站工作,我可以添加新的项目到我的网上商店 我遇到的代码片段如下: $error = ''; $success = ''; if(isset($_POST['submit'])) { define("MAX_SIZE", "4096"); $errors=0; $name = $_POST['name']; $price = $_POST['price']; $desc = filter_var($_POST['d
$error = '';
$success = '';
if(isset($_POST['submit']))
{
define("MAX_SIZE", "4096");
$errors=0;
$name = $_POST['name'];
$price = $_POST['price'];
$desc = filter_var($_POST['desc'], FILTER_SANITIZE_STRING);
$image = $_FILES['image']['name'];
$uploadedfile = $_FILES['image']['tmp_name'];
if($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$error .= '<p class="error">The file must be one of the following file types(jpg|jpeg|png|gif)</p>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if($size > MAX_SIZE*1024)
{
$error .= '<p class="error">The file cannot exceed 4MB in size.</p>';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=128;
$newheight=128;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "../store/images/" . $_FILES['image']['name'];
imagejpeg($tmp,$filename,100);
$fname = $_FILES['image']['name'];
imagedestroy($src);
imagedestroy($tmp);
}
}
if($name == '')
{
$error .= '<p class="error">You must enter a name for the product.</p>';
}
if(!preg_match('/^[a-zA-Z\s]+$/', $name))
{
$error .= '<p class="error">The product name can only contain letters and spaces.</p>';
}
if($_POST['type'] == 'KIT')
{
$type = 'KIT';
}
else if($_POST['type'] == 'VIP')
{
$type = 'VIP';
}
else if($_POST['type'] == 'OTHER')
{
$type = 'OTHER';
}
else
{
$error .= '<p class="error">You must select a valid type for the product.</p>';
}
if($price == '')
{
$error .= '<p class="error">You must enter a price for the product.</p>';
}
if(!preg_match('/^\d+(:?[.]\d{2})$/', $price))
{
$error .= '<p class="error">You must enter a valid price for the product.</p>';
}
if($desc == '')
{
$error .= '<p class="error">You must enter a description for the product.</p>';
}
$stmt = $db->prepare("SELECT * FROM products WHERE name = ?");
if($stmt)
{
$stmt->bind_param('s',$name);
$stmt->execute();
if($stmt->num_rows > 0)
{
$error .= '<p class="error">This product already exists. Please choose a different name.</p>';
}
}
else
{
$error .= '<p class="error">An error occurred at line 135. Please contact the site administrator.</p>';
}
if(empty($error) && !$errors)
{
$stmt = $db->prepare("INSERT INTO products (name, image, price, prod_desc, type) VALUES ( ?, ?, ?, ?, ? )");
if($stmt)
{
$stmt->bind_param('sssss', $name, $fname, $price, $desc, $type);
if($stmt->execute())
{
$success .= '<p class="success">Product added successfully.</p>';
}
else
{
$error .= '<p class="error">An error occured at line 158. Please contact the site administrator.</p>';
}
}
else
{
$error .= '<p class="error">An error occured at line 154. Please contact the site administrator.</p>';
}
}
}
$error='';
$success='';
如果(isset($_POST['submit']))
{
定义(“最大尺寸”、“4096”);
$errors=0;
$name=$_POST['name'];
$price=$_POST['price'];
$desc=filter\u var($\u POST['desc'],filter\u SANITIZE\u字符串);
$image=$_文件['image']['name'];
$uploadedfile=$_文件['image']['tmp_名称'];
如果($image)
{
$filename=stripslashes($_文件['image']['name']);
$extension=getExtension($filename);
$extension=strtolower($extension);
如果(($extension!=“jpg”)&&($extension!=“jpeg”)&&($extension!=“png”)&&($extension!=“gif”))
{
$error.='文件必须是以下文件类型之一(jpg | jpeg | png | gif)
;
$errors=1;
}
其他的
{
$size=filesize($_FILES['image']['tmp_name']);
如果($size>MAX_size*1024)
{
$error.='文件大小不能超过4MB。
';
$errors=1;
}
如果($extension==“jpg”| |$extension==“jpeg”)
{
$uploadedfile=$_文件['image']['tmp_名称'];
$src=imagecreatefromjpeg($uploadedfile);
}
else if($extension==“png”)
{
$uploadedfile=$_文件['image']['tmp_名称'];
$src=imagecreatefrompng($uploadedfile);
}
其他的
{
$src=imagecreatefromgif($uploadedfile);
}
列表($width,$height)=getimagesize($uploadedfile);
$newwidth=128;
$newheight=128;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp、$src、0,0,0、$newwidth、$newheight、$width、$height);
$filename=“../store/images/”$\u文件['image']['name'];
imagejpeg($tmp,$filename,100);
$fname=$\u文件['image']['name'];
(1)(src);
图像处理(tmp);
}
}
如果($name='')
{
$error.='您必须输入产品的名称。
';
}
如果(!preg_match('/^[a-zA-Z\s]+$/',$name))
{
$error.='产品名称只能包含字母和空格。
';
}
如果($_POST['type']=='KIT')
{
$type='KIT';
}
如果($_POST['type']=='VIP')
{
$type='VIP';
}
else if($_POST['type']=='OTHER')
{
$type=‘其他’;
}
其他的
{
$error.='您必须为产品选择有效的类型。
';
}
如果($price='')
{
$error.='您必须输入产品的价格。
';
}
如果(!preg_match('/^\d+(:?[.]\d{2})$/',$price))
{
$error.='您必须输入产品的有效价格。
';
}
如果($desc='')
{
$error.='您必须输入产品的说明。
';
}
$stmt=$db->prepare(“从名称=?”的产品中选择*”;
如果($stmt)
{
$stmt->bind_参数('s',$name);
$stmt->execute();
如果($stmt->num_rows>0)
{
$error.='此产品已存在。请选择其他名称。
;
}
}
其他的
{
$error.='第135行出现错误。请与站点管理员联系。
';
}
if(空($error)&&!$errors)
{
$stmt=$db->prepare(“插入产品(名称、图像、价格、产品描述、类型)值(?,,,?,?)”;
如果($stmt)
{
$stmt->bind_参数('ssssss',$name,$fname,$price,$desc,$type);
如果($stmt->execute())
{
$success.='产品添加成功。
;
}
其他的
{
$error.='第158行出现错误。请与站点管理员联系。
';
}
}
其他的
{
$error.='第154行出现错误。请与站点管理员联系。
';
}
}
}
我遇到问题的部分是if(empty($error)&&&!&errors)语句后面的if($stmt)块
根据我在PHP本身和MySQL insert查询中使用的每个语法检查器,它们都没有报告错误。但是它一直在吐出154行的错误,我不知道为什么。我已经通过回显验证了表单中的变量是否正确存储,我还验证了mysql表的列是否存在,并且拼写是否正确
这让我开始发疯,当我尝试使用$stmt->error或$db->error来回显mysql错误时,返回为空,没有列出任何错误
我得到的唯一结果是,我做了mysqli_errno($db),它返回了一个0
有人知道这段代码有什么问题,或者为什么不起作用吗
表单的HTML标记:
<div id="form">
<form action="" method="post" enctype="multipart/form-data">
<table>
<th colspan="2">Add to Store</th>
<tr>
<td colspan="2">
<p>Please use the form below to add items to the store.</p>
<?php
if($error)
{
echo $error;
}
if($success)
{
echo $success;
}
?>
<hr>
<tr>
<td><label for="name">Product Name:</label></td>
<td><input type="text" name="name" id="name" value="<?php if(isset($name)) { echo $name; } else { echo ''; } ?>" /></td>
</tr>
<tr>
<td><label for="price">Product Price:</label></td>
<td><input type="text" name="price" id="price" size="6" maxlength="6" value="<?php if(isset($price)) { echo $price; } else { echo ''; } ?>" /></td>
</tr>
<tr>
<td><label for="image">Product Image:</label></td>
<td><input type="file" name="image" id="image" /><br /><small>File must be 128x128 pixels, and no larger than 4MB.</small></td>
</tr>
<tr>
<td><label for="type">Product Type:</label></td>
<td>
<select name="type">
<option value="VIP">VIP Package</option>
<option value="KIT">Donator Kit</option>
<option value="OTHER">Other</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><label for="desc">Product Description:</label></td>
</tr>
<tr>
<td colspan="2"><textarea name="desc" style="width: 500px; height:250px;"><?php if(isset($desc)) { echo $desc; } ?></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Add Product" /></td>
</tr>
</table>
</form>
</div>
添加到存储
请使用下表向商店添加物品
产品名称:
所以在经历了很多头痛之后,我设法找到了一个解决办法
问题的根源在于:
$stmt = $db->prepare("SELECT * FROM products WHERE name = ?");
if($stmt)
{
$stmt->bind_param('s',$name);
$stmt->execute();
if($stmt->num_rows > 0)
{
$error .= '<p class="error">This product already exists. Please choose a different name.</p>';
}
}
else
{
$error .= '<p class="error">An error occurred at line 135. Please contact the site administrator.</p>';
}
$stmt=$db->prepare(“从name=?”的产品中选择*”;
如果($stmt)
{
$stmt->bind_参数('s',$name);
$stmt->execute();
如果($stmt->num_rows>0)
{
$error.='此产品已存在。请选择其他名称。
;
}
}
其他的
{
$error.='第135行出现错误。请与站点管理员联系。
';
}
之后
$sql = "SELECT * FROM products WHERE name = '".$name."' LIMIT 1";
$result = $db->query($sql);
if(is_object($result) && $result->num_rows == 1)
{
$error .= '<p class="error">This product already exists. Please choose a different name.</p>';
}
$stmt->bind_param('s',$name);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0)
{
$error .= '<p class="error">This product already exists. Please choose a different name.</p>';
}