簡體   English   中英

PHP的SQL插入問題

[英]SQL Insert Issue with PHP

我有以下代碼將數據插入到數據庫中,但是當某些時候插入數據時,它會成功插入,而在大多數情況下,它會給我以下錯誤。 無法輸入數據:您的SQL語法有誤; 請查看與您的MySQL服務器版本相對應的手冊,以獲取在“拳頭名稱”附近使用的正確語法?”,“ Kabul Janm”,“阿富汗”,“ Kabul”,“ Kabul”,“ 1985-03-26”,“ 1號線的Mal'

有人可以幫我嗎,我需要使其穩定,代碼如下。

 <html>
 <head>
<title>Add New Record in MySQL Database</title>
<script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
<link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
</head>
 <body>
<?php
 include_once ('top.php');
 ?>
 <?php
 include '/Connections/conn.php';
  if(isset($_POST['add']))
   {
  if(! $conn )
  {
  die('Could not connect: ' . mysql_error());
    }

   if(! get_magic_quotes_gpc() )
   {
    $first_name = addslashes ($_POST['first_name']);
    $last_name = addslashes ($_POST['last_name']);
      }
   else
   {
    $first_name = $_POST['first_name'];
     $last_name = $_POST['last_name'];
    }
     $email_address = $_POST['email_address'];
     $phone_no = $_POST['Phone_no'];
     $user_name = $_POST['user_name'];
       $password = $_POST['password'];
       $sec_question = $_POST['sec_question'];
       $Answer = $_POST['Answer'];
       $Country = $_POST['Country'];
       $State = $_POST['State'];
        $city = $_POST['city'];
        $date_birth = $_POST['date_birth'];
       $gender = $_POST['gender'];

         $sql = "INSERT INTO users(first_name, last_name, email_address, Phone_no,        user_name, password, sec_question, Answer, Country, State, city, date_birth, gender) VALUES('$first_name', '$last_name', '$email_address',  '$phone_no', '$user_name', '$password',  '$sec_question', '$Answer', '$Country', '$State', '$city', '$date_birth', '$gender')";
     $dbname;
      $retval = mysql_query( $sql, $conn );
  if(! $retval )
  {
   die('Could not enter data: ' . mysql_error());
   }
   header("Location: /thank.php");
    //echo "<center>Thanks for registration in Mashwani Info Tech Free Online Trainings (MOFT)</center>\n";
     mysql_close($conn);
      }
     else
     {
     ?>
       <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
       <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
       <link href="SpryAssets/SpryValidationPassword.css" rel="stylesheet" type="text/css" />
        <link href="SpryAssets/SpryValidationConfirm.css" rel="stylesheet" type="text/css" />
        <link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
         <script src="SpryAssets/SpryValidationPassword.js" type="text/javascript"></script>
           <script src="SpryAssets/SpryValidationConfirm.js" type="text/javascript"></script>
             <script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>

          <table width="100%" background="/Images/gradient_medium.jpg"> 

         <tr>
           <td width="100%" height="34"> 
            <!--<center> <marquee behavior="Scroll" width="100%" scrollamount="8" direction="Right"><img src="/Images/mtn.jpg" /> <img src="/Images/mtn1.jpg" /></marquee>  </center>
            -->
           </td>
           </tr>
           </table>
            <table width="100%" align="center" bgcolor="#ECF5F0" border="0">
           <tr valign="bottom">    <td height="25"><p>&nbsp;</p>
        <form action="<?php $_PHP_SELF?>" method="post" name="form1" id="form1">
        <table align="center" border="1">
          <tr valign="baseline">
          <td colspan="2" align="left" nowrap="nowrap" bordercolor="#CCCC33">First Name</td>
            <td width="388"><span id="sprytextfield1">
             <input type="text" name="first_name" value="" size="37" id="first_name"/>
             <span class="textfieldRequiredMsg">A value is required.</span></span></td>
             </tr>
             <tr valign="baseline">
                 <td colspan="2" align="left" nowrap="nowrap">Last Name</td>
               <td><input type="text" name="last_name" id="last_name" value="" size="37" /></td>   
              </tr>
              <tr valign="baseline">
                <td colspan="2" align="left" nowrap="nowrap">Email Address</td>
               <td><span id="sprytextfield2">
               <input type="text" name="email_address" id="email_address" value="" size="37" />
                <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
      </tr>
              <tr valign="baseline">
              <td colspan="2" align="left" nowrap="nowrap">Phone No <font size="-4" color="#00CC66">(0093772221521)</font></td>
             <td><span id="sprytextfield3">
             <input type="text" name="Phone_no" id="Phone_no" value="" size="37" />
             <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
             </tr>
             <tr valign="baseline">
             <td colspan="2" align="left" nowrap="nowrap">User Name</td>
             <td><span id="sprytextfield4">
              <input type="text" name="user_name" id="user_name" value="" size="37" />
              <span class="textfieldRequiredMsg">A value is required.</span></span></td>
               </tr>
              <tr valign="baseline">
             <td colspan="2" align="left" nowrap="nowrap">Password <font size="-4" color="#00CC66">(Min 8 Charectors) </font></td>
            <td><span id="pass">
           <input type="password" name="password" value="" size="37" id="password" />
        <span class="passwordRequiredMsg">A value is required.</span><span class="passwordInvalidStrengthMsg">The password doesn't meet the specified strength.</span></span></td>
      </tr>
        <tr valign="baseline">
        <td colspan="2" align="left" nowrap="nowrap">Confirm Password:</td>
        <td><span id="spryconfirm1">
          <label for="confirm"></label>
          <input name="confirm" type="password" id="confirm" size="37" />
          <span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>
      </tr>

      <tr valign="baseline">
        <td colspan="2" align="left" nowrap="nowrap">Secret Question</td>
        <td><span id="spryselect1">
          <label for="sec"></label>
          <select name="sec_question" id="sec_question">
            <option value="What is your fist school name?">What is your fist school name?</option>
            <option value="Where did your birth happened?">Where did your birth happened?</option>
            <option value="What is your father's fist name?">What is your father's fist name?</option>
            <option value="Where did you get your degree?">Where did you get your degree?</option>
          </select>
        <span class="selectRequiredMsg">Please select an item.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="left" nowrap="nowrap">Answer for Question</td>
        <td><span id="sprytextfield5">
          <input type="text" name="Answer" id="Answer" value="" size="37" />
        <span class="textfieldRequiredMsg">A value is required.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td width="61" rowspan="3" align="left" valign="middle" nowrap="nowrap">Address</td>
        <td width="121" align="left" nowrap="nowrap">Country</td>
        <td><input type="text" name="Country" id="Country" value="" size="37" /></td>
      </tr>
      <tr valign="baseline">
        <td width="121" align="left" nowrap="nowrap">State</td>
        <td><span id="sprytextfield6">
          <input type="text" name="State" id="State" value="" size="37" />
        <span class="textfieldRequiredMsg">A value is required.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td width="121" align="left" nowrap="nowrap">City</td>
        <td><span id="sprytextfield7">
          <input type="text" name="city" id="city" value="" size="37" />
        <span class="textfieldRequiredMsg">A value is required.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="left" nowrap="nowrap">Date of Birth <font size="-4" color="#00CC66"> (YYYY-MM-DD) </font></td>
        <td><span id="sprytextfield8">
        <input type="text" name="date_birth" id="date_birth" value="" size="37" />
        <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td colspan="2" align="left" nowrap="nowrap">Gender</td>
        <td><span id="spryselect2">
          <label for="gen"></label>
          <select name="gender" id="gender">
            <option value="Select your gender here." selected="selected">Select your gender here.</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
          </select>
        <span class="selectRequiredMsg">Please select an item.</span></span></td>
      </tr>
    </table>
    <center> <input name="add" type="submit" value="Sign Up" id="add" /> </center>
    <input type="hidden" name="ID" value="" />
    <input type="hidden" name="admin_level" id="admin_level" value="" />
    <input type="hidden" name="time_stamp" id="time_stamp" value="" />
    <input type="hidden" name="MM_insert" value="form1" />
  </form>
  <p>&nbsp;</p></td>
    </tr>

    </table>
    <?php
    }
   ?>    
    <script type="text/javascript">
   var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {validateOn:["blur"]});
    var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "email");
    var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "phone_number", {format:"phone_custom"});
    var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
    var sprypassword1 = new Spry.Widget.ValidationPassword("pass", {minAlphaChars:1, minUpperAlphaChars:1, minSpecialChars:1, validateOn:["blur"]});
    var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "password", {validateOn:["blur"]});
      var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");
      var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5");
      var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6");
       var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");
        var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "date", {format:"yyyy-mm-dd"});
        var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2");
        var CollapsiblePanel1 = new Spry.Widget.CollapsiblePanel("CollapsiblePanel1");
         </script>
        <?php
        include_once ('bottom.php');
         ?>
        </body>
         </html>

在查詢中使用數據之前,必須轉義“'”符號。

發生的情況是您的用戶在輸入字段中輸入“'”,而MySQL出現錯誤。

使用准備好的語句以自動方式處理該問題。

是示例。

編輯:

像這樣編輯代碼:

if(! get_magic_quotes_gpc() )
{
    $first_name = addslashes ($_POST['first_name']);
    $last_name = addslashes ($_POST['last_name']);
    $email_address = addslashes ($_POST['email_address']);
    $phone_no = addslashes ($_POST['Phone_no']);
    $user_name = addslashes ($_POST['user_name']);
    $password = addslashes ($_POST['password']);
    $sec_question = addslashes ($_POST['sec_question']);
    $Answer = addslashes ($_POST['Answer']);
    $Country = addslashes ($_POST['Country']);
    $State = addslashes ($_POST['State']);
    $city = addslashes ($_POST['city']);
    $date_birth = addslashes ($_POST['date_birth']);
    $gender = addslashes ($_POST['gender']);
}
else
{
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email_address = $_POST['email_address'];
    $phone_no = $_POST['Phone_no'];
    $user_name = $_POST['user_name'];
    $password = $_POST['password'];
    $sec_question = $_POST['sec_question'];
    $Answer = $_POST['Answer'];
    $Country = $_POST['Country'];
    $State = $_POST['State'];
    $city = $_POST['city'];
    $date_birth = $_POST['date_birth'];
    $gender = $_POST['gender'];
}

它應該防止錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM