mysqli_query无法将数据插入数据库

时间:2016-12-29 11:12:37

标签: mysqli database-connection insert-into

我的问题似乎很普遍,但我仍然无法通过广泛的网络搜索找到正确的解决方案,因为我是网络开发的新手。

我已查看How can I prevent SQL injection in PHP?但无法解决此问题。

$ Connection工作正常并且没有错误,除非我在 $connection = mysqli_connect

中更改数据库名称,用户名,密码等

这是我的输入表:

<pre><form id="sales" onsubmit="return checkdate(this);" action="sales.php" method="post">
Invoice_Number *  <input type="number" name="invnum"  required><br>
Date *            <input type="date" name="date" id="date" required>format: (mm-dd-yyyy)<br>
job_id            <input type="text" id="job_id" name="job_id"><br>
Description       <input type="text" name="descr"><br>
Price *           <input type="number" name="price" required><br>
Quantity *        <input type="number" name="qty" required><br>
Total Amount *    <input type="number" name="total"><br>
                  <input id="submit" type="submit" name="submit" value="Submit"><br></form></pre>

这是用于从表单输入定义输入值的PHP代码。

if (isset($_POST['invnum'])){$invnum = $_POST['invnum'];} Else { $invnum ="";}
if (isset($_POST['date'])){$date = $_POST['date'];} Else { $date = "";}
if (isset($_POST['job_id'])){$job_id = $_POST['job_id'];} Else { $job_id = "";}
if (isset($_POST['descr'])){$descr = $_POST['descr'];} Else { $descr = "";}
if (isset($_POST['price'])){$price = $_POST['price'];} Else { $price = "";}
if (isset($_POST['qty'])){$qty = $_POST['qty'];} Else { $qty = "";}
if (isset($_POST['total'])){$total = $_POST['total'];} Else { $total = "";}
echo "{$invnum}: {$date}: {$job_id}: {$descr}: {$price}: {$qty}: {$total}";
if (isset($_POST['submit'])){echo "Form is submitted.";} Else { echo "Form not submitted.";}

以下是将数据插入数据库的代码。 注意:我使用的是Sublime Text。在INSERT INTO字段中有一个奇怪的行为,括号中的所有其他字段都是黄色,但 日期 字段是蓝色和斜体。

$query = "INSERT INTO sales(job_id, date, descr, price, qty, total) VALUES ($job_id, $date, '$descr', $price, $qty, $total)";

$invoice_set = mysqli_query($connection, $query);

以下是测试我尝试查找错误但没有错误,除了&#34;数据库查询失败&#34;。

echo 'Here is the result:'. $invoice_set;

echo 'Here are query inputs:'. $query;

$function confirm_query($invoice_set){if (!$invoice_set){die ("database query failed");}}

if (!mysqli_query($connection, "SET a=1")) {printf("Errormessage: %s\n", mysqli_error($connection));}

我已经付出了很大的努力来达到这一点。请帮助,我将非常感激。

0 个答案:

没有答案