Php 准备好的语句未发送到数据库
我正在尝试创建我的第一个准备好的语句。有了这个,我也尝试发送我的第一个图像文件到我的数据库中。我已经在这方面工作了一个月左右,无法让它工作。我最初试图用php的一个简单的insert命令来实现这一点,除了img文件的名称之外,所有的东西都被插入到我的数据库中。我被多次告知要用一份准备好的声明来创建这个,所以我正在这样做,但是没有任何东西发送到我的数据库,我也不知道为什么 我在代码开始时对一些数据字段进行验证。我还没有验证img文件,但稍后会验证。验证不是问题,因为我把它分解了一点,它正在通过它 我没有任何正在发生的错误。我甚至加入了一些东西,在执行语句时会产生语法错误,但不会产生任何错误 有没有人看到我在这里做错了什么,导致这封邮件无法发送?这与我的img文件有关,还是我做的陈述不正确Php 准备好的语句未发送到数据库,php,html,image,prepared-statement,Php,Html,Image,Prepared Statement,我正在尝试创建我的第一个准备好的语句。有了这个,我也尝试发送我的第一个图像文件到我的数据库中。我已经在这方面工作了一个月左右,无法让它工作。我最初试图用php的一个简单的insert命令来实现这一点,除了img文件的名称之外,所有的东西都被插入到我的数据库中。我被多次告知要用一份准备好的声明来创建这个,所以我正在这样做,但是没有任何东西发送到我的数据库,我也不知道为什么 我在代码开始时对一些数据字段进行验证。我还没有验证img文件,但稍后会验证。验证不是问题,因为我把它分解了一点,它正在通过它
if($validation->passed()) {
if(isset($_POST['create'])){
$product_id = trim( $_POST['product_id'] );
$name = trim( $_POST['name'] );
$price = trim( $_POST['price'] );
$saleprice = trim( $_POST['saleprice'] );
$final_price = trim( $_POST['final_price'] );
$shippingprice = trim( $_POST['shippingprice'] );
$category = trim( $_POST['category'] );
$item_details = trim( $_POST['item_details'] );
$item_details2 = trim( $_POST['item_details2'] );
$description = trim( $_POST['description'] );
$viewproduct_type = trim( $_POST['viewproduct_type'] );
$file = "productpics/". $_FILES['file']['name']; // save the filename
}else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
move_uploaded_file($_FILES['file']['tmp_name'], $file); // move the file
//Connection
$con = mysqli_connect("localhost","root","","bfb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "INSERT INTO products (product_id, name, price, saleprice, final_price, shippingprice, category, item_details, item_details2, description, viewproduct_type, date, img) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)")) {
/* bind parameters for markers */
$stmt->bind_param('isiiiissssss', $product_id, $name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $file);
/* execute query */
$stmt->execute();
//if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}
/* close statement */
mysqli_stmt_close($stmt);
}
}
}
}
}
?>
if($validation->passed()){
如果(isset($_POST['create']){
$product_id=修剪($_POST['product_id']);
$name=trim($_POST['name']);
$price=trim($_POST['price']);
$saleprice=trim($_POST['saleprice']);
$final_price=trim($u POST['final_price']);
$shippingprice=trim($_POST['shippingprice']);
$category=修剪($_POST['category']);
$item_details=修剪($_POST['item_details']);
$item_details2=修剪($u POST['item_details2']);
$description=修剪($_POST['description']);
$viewproduct_type=修剪($_POST['viewproduct_type']);
$file=“productpics/”$\u FILES['file']['name'];//保存文件名
}否则{
foreach($validation->errors()作为$error){
回显$error,“
”;
}
移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\名称,$file);//移动文件
//联系
$con=mysqli_connect(“localhost”、“root”、“bfb”);
/*检查连接*/
if(mysqli\u connect\u errno()){
printf(“连接失败:%s\n”,mysqli_Connect_error());
退出();
}
/*创建准备好的语句*/
如果($stmt=mysqli_prepare($con),“插入到产品中(产品id、名称、价格、售价、最终价格、发货价格、类别、项目详细信息、项目详细信息2、说明、视图产品类型、日期、img)值(?、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、{
/*标记的绑定参数*/
$stmt->bind_参数($ISIIISSSSS',$product_id,$name,$price,$saleprice,$final_price,$shippingprice,$category,$item_details,$item_details2,$description,$viewproduct_type,$file);
/*执行查询*/
$stmt->execute();
//如果(!$stmt->execute()){trigger_error(“有一个错误…”,“$con->error,E_USER_WARNING);}
/*结束语*/
mysqli_stmt_close($stmt);
}
}
}
}
}
?>
形式
<form action="" method="POST" enctype="multipart/form-data">
<div class="field">
<label for="product_id">Product ID</label>
<input type="text" name="product_id" class="smallinputbar" required>
</div>
<div class="field">
<label for="name">Product Name</label>
<input type="text" class="inputbar" name="name" required>
</div>
<div class="field">
<label for="price">Product Price</label>
<input type="text" class="smallinputbar" name="price" required>
</div>
<div class="field">
<label for="saleprice">Sale Price</label>
<input type="text" class="smallinputbar" name="saleprice">
</div>
<div class="field">
<label for="final_price">Final Price</label>
<input type="text" class="smallinputbar" name="final_price" required>
</div>
<div class="field">
<label for="shippingprice">Shipping Price</label>
<input type="text" class="smallinputbar" name="shippingprice" required>
</div>
<div class="field">
<label for="category">Category</label>
<input type="text" class="inputbar" name="category" required>
</div>
<div class="field">
<label for="item_details">Item Details</label>
<input type="message" class="messageinput" name="item_details" required>
</div>
<div class="field">
<label for="item_details2">Item Details 2</label>
<input type="message" class="messageinput" name="item_details2">
</div>
<div class="field">
<label for="description">Description</label>
<input type="message" class="messageinput" name="description" required>
</div>
<div class="field">
<label for="viewproduct_type">View Product Type</label>
<select class="optionbar" name="viewproduct_type">
<option value="Not Selected">Not Selected</option>
<option value="a href='./viewProduct.php?view_product=$id">Standard</option>
<option value="Option">Option</option>
</select>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<label for="button">
<input type="submit" id="button" name="create" value="Create New Product">
</label>
<span class="floatright">
<input type="file" name="file" class="inputbarfile">
<input type="submit" name="create" id="signinButton" value="Upload">
</span>
</form>
产品ID
品名
产品价格
销售价格
最终价格
运费
类别
项目详细信息
项目详情2
描述
查看产品类型
未选择
标准
选项
你没有像我告诉你的那样检查你的查询中的错误。你说了,为什么?用我在答案里写的东西。没有错误,或者这无关紧要?我查过了。它没有抛出任何错误,因此我在这里对它进行了注释,以表明我检查了它。请尝试删除if($stmt=mysqli\u prepare($con…
的条件语句,然后将其替换为$stmt=$con->prepare(…
看看这是否有任何区别。或者,尝试一个简单的“一项”查询而不是一大堆。一旦成功,就从那里开始构建。一旦遇到障碍,你就知道该怎么做了。我还要检查一下,从if($validation->passed()){
开始,你的第一个代码体似乎缺少2个右大括号}
如果这是它的全部代码。我还看到2个输入带有相同的name=“create”
属性,所以这是有问题的。我对上面的注释进行了编辑,重新加载它们。尤其是最后一个。
<?php
//Validation
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'product_id' => array(
'required' => true,
'min' => 1,
'max' => 50,
'unique' => 'products'
),
'name' => array (
'required' => true,
'min' => 2,
'max' => 50
),
'price' => array (
'required' => true,
'min' => 1,
'max' => 50
),
'saleprice' => array (
'min' => 1,
'max' => 50
),
'final_price' => array (
'required' => true,
'min' => 1,
'max' => 50
),
'shippingprice' => array (
'max' => 50
),
'category' => array (
'required' => true,
'min' => 2,
'max' => 50
),
'item_details' => array (
'required' => true,
'min' => 2,
'max' => 1550
),
'item_details2' => array (
'max' => 1550
),
'description' => array (
'required' => true,
'min' => 2,
'max' => 1550
)
)
);
//Create
if($validation->passed()) {
if(isset($_POST['create'])){
$product_id = trim( $_POST['product_id'] );
$name = trim( $_POST['name'] );
$price = trim( $_POST['price'] );
$saleprice = trim( $_POST['saleprice'] );
$final_price = trim( $_POST['final_price'] );
$shippingprice = trim( $_POST['shippingprice'] );
$category = trim( $_POST['category'] );
$item_details = trim( $_POST['item_details'] );
$item_details2 = trim( $_POST['item_details2'] );
$description = trim( $_POST['description'] );
$viewproduct_type = trim( $_POST['viewproduct_type'] );
$file = "productpics/". $_FILES['file']['name']; // save the filename
}else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
move_uploaded_file($_FILES['file']['tmp_name'], $file); // move the file
//Connection
$con = mysqli_connect("localhost","root","","bfb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create a prepared statement */
$stmt = $con->prepare("INSERT INTO products (product_id, name, price, saleprice, final_price, shippingprice, category, item_details, item_details2, description, viewproduct_type, date, img) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)")
/* bind parameters for markers */
$stmt->bind_param('isiiiissssss', $product_id, $name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $file);
/* execute query */
if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}
/* close statement */
mysqli_stmt_close($stmt);
}
}
}
}
?>