2017-05-05 47 views
-1

我试图插入/编辑数据库记录,和我下面这个例子:http://www.killersites.com/community/index.php?/topic/3064-basic-php-system-view-edit-add-delete-records-with-mysqli/与mysqli的准备语句INSERT INTO/UPDATE数据库记录PHP形式

不过,我似乎无法得到新的记录插入到数据库。这是我到目前为止:

<?php 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 
// Allow the user to both create new records and edit existing records. 
// Connect to the database. 
$connect = mysqli_connect('localhost', 'user', 'password', 'database_name'); 

// creates the new/edit record form. 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 

function renderForm($error = '', $id = '', $start_date = '', $company = '', $stock_code = '', $card_quantity = '', $fiske_print = '', $carrier_quantity = '', $quoted = '', $quote_details = '', $quoted_date = '', $quote_accepted = '', $quote_accepted_date = '', $proof_sent = '', $proof_sent_date = '', $proof_approved = '', $proof_approved_date = '', $reminder_date = '', $printed = '', $print_date = '', $closed_loop_allocated = '', $invoiced = '', $invoiced_date = '', $posted = '', $tracking_number = '', $postal_date = '', $paid = '', $is_bulk_load = '', $bulk_funds_recieved = '', $cards_loaded = '', $notes = '', $completed = '') 
{ 
?> 

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title></title> 
<link rel="stylesheet" type="text/css" href="css/style.css"> 

<style> 
table { 
    width: 60% 
} 
table td:nth-child(1){ 
    width: 20%; 
    text-align: right; 
} 
table td:nth-child(2){ 
    width: 30%; 
} 
table td:nth-child(3){ 
    width: 20%; 
    text-align: right; 
} 
table td:nth-child(4){ 
    width: 30%; 
} 
#billdesc{ 

} 
#test{ 
    width: 100px; 
    height: 30px; 
} 
option { 
    height: 30px; 
    line-height: 30px; 
} 
.editOption{ 
    width: 150px; 
    height: 26px; 
    position: relative; 
    background: #fff; 
    border: 1; 
    padding-left: 5px; 
} 
</style> 
</head> 

<body> 
<?php 
    $pageName = 'overview'; 
?> 

<?php 
    include('header.php'); 
?> 

<h1><?php 
    if ($id != '') { 
     echo "Edit Record"; 
    } else { 
     echo "New Record"; 
    } 
?></h1> 
<?php 
    if ($error != '') { 
     echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; 
    } 
?> 

<form action="" method="post"> 
<div> 

    <table> 
     <tr> 
      <td colspan="2" style="text-align:center;"><strong>Job Details</strong></td> 
      <td colspan="2" style="text-align:center;"><strong>Job Progress</strong></td> 
     </tr> 
     <tr> 
      <td>ID: </td> 
      <td><input type="text" name="id" readonly></td> 
      <td>Quoted: </td> 
      <td><input type="checkbox" name="quoted" value=""></td> 
     </tr> 
     <tr> 
      <td>Start Date: </td> 
      <td><input type="date" name="start_date"></td> 
      <td>Quote Details: </td> 
      <td><input type="text" name="quote_details" size="40"></td> 
     </tr> 
     <tr> 
      <td>Company: </td> 
      <td><input type="text" name="company" size="40"></td> 
      <td>Quote Date: </td> 
      <td><input type="date" name="quote_date"></td> 
     </tr> 
     <tr> 
      <td>Stock Code: </td> 
      <td> 
       <div id="billdesc"> 
        <select id="test"> 
         <option class="non" value="GS01">GS01</option> 
         <option class="non" value="GS03">GS03</option> 
         <option class="non" value="RELOAD">Reload</option> 
         <option class="non" value="SM01">SM01</option> 
         <option class="non" value="SM11">SM11</option> 
         <option class="non" value="CG01">CG01</option> 
         <option class="non" value="CG38">CG38</option> 
         <option class="editable" value="Other">Other</option> 
        </select> 
        <input class="editOption" style="display:none;" placeholder="Enter Stock Code"></input> 
       </div> 
      </td> 
      <td>Quote Accepted: </td> 
      <td><input type="checkbox" name="quote_accepted" value=""></td> 
     </tr> 
     <tr> 
      <td>Card Quantity: </td> 
      <td><input type="text" name="card_quantity"></td> 
      <td>Quote Accepted Date: </td> 
      <td><input type="date" name="quote_accepted_date"></td> 
     </tr> 
     <tr> 
      <td>Carrier Quantity: </td> 
      <td><input type="text" name="carrier_quantity"></td> 
      <td>Proof Sent: </td> 
      <td><input type="checkbox" name="proof_sent" value=""></td> 
     </tr> 
     <tr> 
      <td>Fiske Print: </td> 
      <td><input type="checkbox" name="fiske_print" value=""></td> 
      <td>Proof Sent Date: </td> 
      <td><input type="date" name="proof_sent_date"></td> 
     </tr> 
     <tr> 
      <td rowspan="6" colspan="2" style="text-align:center;"> 
       Notes:<br> 
       <textarea name="message" rows="8" cols="70"></textarea> 
      </td> 
      <td style="text-align:right;">Proof Approved: </td> 
      <td style="text-align:left;"><input type="checkbox" name="proof_approved" value=""></td> 
     </tr> 
     <tr> 
      <td>Proof Approved Date: </td> 
      <td><input type="date" name="proof_approved_date"></td> 
     </tr> 
     <tr> 
      <td>Printed: </td> 
      <td><input type="checkbox" name="printed" value=""></td> 
     </tr> 
     <tr> 
      <td>Print Date</td> 
      <td><input type="date" name="printed_date"></td> 
     </tr> 
     <tr> 
      <td>Closed Loop Allocated: </td> 
      <td><input type="checkbox" name="closed_loop_allocated" value=""></td> 
     </tr> 
     <tr> 
      <td>Invoiced: </td> 
      <td><input type="checkbox" name="invoiced" value=""></td> 
     </tr> 
     <tr> 
      <td>Paid: </td> 
      <td><input type="checkbox" name="paid" value=""></td> 
      <td>Invoice Date: </td> 
      <td><input type="date" name="invoice_date"></td> 
     </tr> 
     <tr> 
      <td>Is Bulk Load: </td> 
      <td><input type="checkbox" name="is_bulk_load" value=""></td> 
      <td>Posted: </td> 
      <td><input type="checkbox" name="posted" value=""></td> 
     </tr> 
     <tr> 
      <td>Bulk Funds Recieved</td> 
      <td><input type="checkbox" name="bulk_funds_recieved" value=""></td> 
      <td>Postal Date: </td> 
      <td><input type="date" name="postal_date"></td> 
     </tr> 
     <tr> 
      <td>Cards Loaded: </td> 
      <td><input type="checkbox" name="cards_loaded" value=""></td> 
      <td>Tracking Number: </td> 
      <td><input type="text" name="tracking_number" size="30"></td> 
     </tr> 
     <tr> 
      <td colspan="4" style="text-align:center;"> 
       Completed: <input type="checkbox" name="completed" value="">&nbsp;&nbsp; 
       <input type="submit" name="submit" value="Save" style="width:90px" /> 
      </td> 
     </tr> 
    </table> 

<script type="text/javascript"> 
var initialText = $('.editable').val(); 
$('.editOption').val(initialText); 

$('#test').change(function(){ 
var selected = $('option:selected', this).attr('class'); 
var optionText = $('.editable').text(); 

if(selected == "editable"){ 
    $('.editOption').show(); 


    $('.editOption').keyup(function(){ 
     var editText = $('.editOption').val(); 
     $('.editable').val(editText); 
     $('.editable').html(editText); 
    }); 

}else{ 
    $('.editOption').hide(); 
} 
}); 
</script> 

</body> 
</html> 

<?php 
} 

/* 

EDIT RECORD 

*/ 
// if the 'id' variable is set in the URL, we know that we need to edit a record 
if (isset($_GET['id'])) { 
    // if the form's submit button is clicked, we need to process the form 
    if (isset($_POST['submit'])) { 

    // I'll get to this bit next... first figure out how to add new record. 
} 

/* 

NEW RECORD 

*/ 
// if the 'id' variable is not set in the URL, we must be creating a new record 
else { 
    // if the form's submit button is clicked, we need to process the form 
    if (isset($_POST['submit'])) { 
     // get the form data 
     $start_date = date("Y-m-d", strtotime($_GET['start_date'])); 
     $company = htmlentities($_POST['company'], ENT_QUOTES); 
     $stock_code = htmlentities($_POST['stock_code'], ENT_QUOTES); 
     $card_quantity = htmlentities($_POST['card_quantity'], ENT_QUOTES); 
     $carrier_quantity = htmlentities($_POST['carrier_quantity'], ENT_QUOTES); 
     $fiske_print = htmlentities($_POST['fiske_print'], ENT_QUOTES); 
     $quoted = htmlentities($_POST['quoted'], ENT_QUOTES); 
     $quote_details = htmlentities($_POST['quote_details'], ENT_QUOTES); 
     $quote_date = date("Y-m-d", strtotime($_GET['quote_date'])); 
     $quote_accepted = htmlentities($_POST['quote_accepted'], ENT_QUOTES); 
     $quote_accepted_date = date("Y-m-d", strtotime($_GET['quote_accepted_date'])); 
     $proof_sent = htmlentities($_POST['proof_sent'], ENT_QUOTES); 
     $proof_sent_date = date("Y-m-d", strtotime($_GET['proof_sent_date'])); 
     $proof_approved = htmlentities($_POST['proof_approved'], ENT_QUOTES); 
     $proof_approved_date = date("Y-m-d", strtotime($_GET['proof_approved_date'])); 
     $printed = htmlentities($_POST['printed'], ENT_QUOTES); 
     $printed_date = date("Y-m-d", strtotime($_GET['printed_date'])); 
     $closed_loop_allocated = htmlentities($_POST['closed_loop_allocated'], ENT_QUOTES); 
     $invoiced = htmlentities($_POST['invoiced'], ENT_QUOTES); 
     $invoice_date = date("Y-m-d", strtotime($_GET['invoice_date'])); 
     $posted = htmlentities($_POST['posted'], ENT_QUOTES); 
     $postal_date = date("Y-m-d", strtotime($_GET['postal_date'])); 
     $tracking_number = htmlentities($_POST['tracking_number'], ENT_QUOTES); 
     $paid = htmlentities($_POST['paid'], ENT_QUOTES); 
     $is_bulk_load = htmlentities($_POST['is_bulk_load'], ENT_QUOTES); 
     $bulk_funds_received = htmlentities($_POST['bulk_funds_received'], ENT_QUOTES); 
     $cards_loaded = htmlentities($_POST['cards_loaded'], ENT_QUOTES); 
     $completed = htmlentities($_POST['completed'], ENT_QUOTES); 


     // insert the new record into the database 
     if ($stmt = $connect->prepare("INSERT INTO jobs (start_date,company,stock_code,card_quantity,fiske_print,carrier_quantity,quoted,quote_details,quoted_date,quote_accepted,quote_accepted_date,proof_sent,proof_sent_date,proof_approved,proof_approved_date,printed,print_date,closed_loop_allocated,invoiced,invoiced_date,posted,tracking_number,postal_date,paid,is_bulk_load,bulk_funds_received,cards_loaded,notes,completed) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")) { 
      $stmt->bind_param("sssssssssssssssssssssssssssss",$start_date,$company,$stock_code,$card_quantity,$fiske_print,$carrier_quantity,$quoted,$quote_details,$quote_date,$quote_accepted,$quote_accepted_date,$proof_sent,$proof_sent_date,$proof_approved,$proof_approved_date,$printed,$printed_date,$closed_loop_allocated,$invoiced,$invoice_date,$posted,$tracking_number,$postal_date,$paid,$is_bulk_load,$bulk_funds_received,$cards_loaded,$notes,$completed); 
      $stmt->execute(); 
      $stmt->close(); 
     } 
     // show an error if the query has an error 
     else { 
      echo "ERROR: Could not prepare SQL statement."; 
     } 

     // redirect the user 
     header("Location: addJob.php"); 

    } 
    // if the form hasn't been submitted yet, show the form 
    else { 
     renderForm(); 
    } 
} 

// close the mysqli connection 
$connect->close(); 
?> 

我看不出为什么它不插入新的记录。任何援助将不胜感激。

+0

()',然后'bind_param(“...”,$ variable)',**然后**定义'$ variable',并在所有运行'execute()'之后。看起来像'bind_param()'只是说“从这个变量读取”,但它不会在你运行execute()之前取得它的值。 –

+2

检查[mysqli_stmt :: execute](http://php.net/manual/en/mysqli-stmt.execute.php)的第一个示例。作为旁注,您不希望将修改过的数据保存到数据库中('htmlentities()'等),而是在读取数据**时格式化数据**。 –

+0

@AlejandroIván由于'bind_param'使用了引用,所以可以在分配前进行绑定。当你想在循环中执行查询时,这很有用。您可以在循环之前准备并绑定一次,然后分配变量并在循环中调用'execute'。 – Barmar

回答

0
$company = htmlentities($_POST['company'], ENT_QUOTES); 

如果您使用SQL查询参数,则不需要进行任何转义。这是使用查询参数的要点。

我看到您的SQL查询有29列,但只有28 ?占位符。你只是一个。

提示:MySQL支持的替代INSERT的语法是这样的:

INSERT INTO jobs SET start_date=?, company=?, stock_code=?, ... 

而不是使用VALUES子句和紧张,你的眼睛努力的?占位符的数量相匹配的列数,只是它们配对随着你使用这种语法继续前进。详细请参照https://dev.mysql.com/doc/refman/5.7/en/insert.html

虽然它不支持多行INSERT,但它要容易得多。

另一个技巧是记录MySQL的生成错误:

... 
// show an error if the query has an error 
else { 
    error_log('SQL error ('.__FILE__.' line '.__LINE__.'): '. $connect->error); 
    echo "ERROR: Could not prepare SQL statement."; 
} 

你应该已经看到了这个错误,并能自行诊断问题:我相信你有`准备

ERROR 1136 (21S01): Column count doesn't match value count at row 1 
+0

感谢您的建议@Bill。 – MikeA