无错误无数据存储到数据库中

时间:2014-12-03 06:17:03

标签: php mysql database

运行此代码时没有收到任何错误,但即使没有数据存储到数据库中。所有数据库连接都有效请帮助解决这个谜。数据库结构也有17个字段,我检查了适当的数据类型。

表单页面编码

<?php include('config.php');
if(isset($_POST['submit']))
{
$purchase_date=$_POST['purchase_date'];
$assession_no=$_POST['assession_no'];
$book_title=$_POST['book_title'];
$edition=$_POST['edition'];
$publisher=$_POST['publisher'];
$year_of_publish=$_POST['year_of_publish'];
$volume=$_POST['volume'];
$number_of_pages=$_POST['number_of_pages'];
$source=$_POST['source'];
$cost=$_POST['cost'];
$quantity=$_POST['quantity'];
$note=$_POST['note'];
$category=$_POST['category'];   

$sql=mysql_query("insert into add_book(b_id,purchase_date,assession_no,book_title,edition,publisher,year_of_publish,volume,number_of_pages,source,cost,quantity,note,category) values('','$purchase_date','$assession_no','$book_title','$edition','$publisher','$year_of_publish','$volume','$number_of_pages','$source','$cost','$quantity','$note','$category')");

} ?>

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action=""  class="basic-grey" >
     <label>
        <span>Purchase Date:</span>
        <input name="purchase_date" type="text" class="datepicker" id="popup_container" required/>
    </label>
     <label>
        <span>Assession No. :</span>
        <input name="assession_no" type="text" value="" required/>
    </label>

    <label>
        <span>Book Title :</span>
        <input  type="text" name="book_title" id="title" placeholder="" value="" required/>

    </label>
     <label>
        <span>Edition :</span>
        <input name="edition" type="text" value="" required/>
    </label>

     <label>
        <span>Publisher :</span>
        <input name="publisher" type="text" value="" required/>
    </label>

   <label>
        <span>Year of Publish:</span>
        <input type="text"  name="year_of_publish" value="" required/>
    </label>

         <label>
        <span>Volume:</span>
        <input name="volume" type="text" value="" required/>
    </label>

    <label>
        <span>Page:</span>
        <input name="number_of_pages" type="text" value="" required/>
    </label>

    <label>
        <span>Source:</span>
        <input name="source" type="text" value="" required/>
    </label>
    <label>
        <span>Cost:</span>
        <input name="cost" type="text" value="" required/>
    </label>

      <label>
        <span>Quantity :</span>
        <input name="quantity" type="text" value="" required/>
    </label>

    <label>
        <span>General Note :</span>
        <textarea id="message" name="note" placeholder="" value=""></textarea>
    </label> 
     <label>
        <span>Category :</span><select name="category" required>
        <option value="Engineering">Engineering</option>
        <option value="General">General</option>
        <option value="Competitive">Competitive</option>
        <option value="Reading Space">Reading Space</option>
        </select>
    </label> 


                                                    <label>Image Upload</label>
                                                    <input type="file"  name="photo" /></span>
                                                                <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>                                                       





     <label>
        <span>&nbsp;</span> 
        <input type="submit" name="submit" class="button" /> 
    </label>    
</form>
</body>
</html>

Config.php编码

<?php
$con=mysql_connect("localhost","root","") ;
$db=mysql_select_db("friendslib",$con) ; 
?>

5 个答案:

答案 0 :(得分:1)

将它放在页面顶部 使用error_reporting(E_ALL); ini_set(&#39; display_errors&#39;,1);

并将表单类型设置为post和 $ sql = mysql_query(&#34;你的代码&#34;)或死(mysql_error());

答案 1 :(得分:0)

您没有处理任何错误!你怎么知道你没有收到任何错误?

将其放在页面顶部:

error_reporting(-1);
ini_set('display_errors', 'On');

额外注意:

不要使用mysql函数。你需要使用mysqli或PDO。

使用此代码:

$sql=mysql_query("insert into add_book(purchase_date,assession_no,book_title,edition,publisher,year_of_publish,volume,number_of_pages,source,cost,quantity,note,category) values('$purchase_date','$assession_no','$book_title','$edition','$publisher','$year_of_publish','$volume','$number_of_pages','$source','$cost','$quantity','$note','$category')");

}

注意:

我发现您的查询b_id中有but you are leaving it empty in the values!所以我修好了。我希望它有所帮助。

第二次修改:

您发布的错误,显示mysqli错误,但您使用的是mysql!您无法将mysqlimysql一起使用。

请您确保在config.php和other_php_page.php中使用mysqli或mysql?

答案 2 :(得分:0)

这是错误:

<form action=""  class="basic-grey" >

将其更改为:

<form action=""  class="basic-grey" method="post">

Default form methodget,如果您未设置,则假定为get

您正在检查$_POST['submit'],因为此而永远不会设置。

注意:

不使用mysql函数,它们已被弃用,将在下一版本的PHP中删除。你需要使用mysqli或PDO。

答案 3 :(得分:0)

你不处理任何事情必须填写这样的行动和方法:

<form action="NAME OF THIS PAGE like page.php" method="post" >
</form>

`

答案 4 :(得分:0)

使用以下代码解决错误:

<?php 

$con=mysqli_connect("localhost","root","", "friendslib") ;
error_reporting(-1);

ini_set('display_errors', 'On');

if(isset($_POST['submit']))
{
$purchase_date=$_POST['purchase_date'];
$assession_no=$_POST['assession_no'];
$book_title=$_POST['book_title'];
$edition=$_POST['edition'];
$publisher=$_POST['publisher'];
$year_of_publish=$_POST['year_of_publish'];
$volume=$_POST['volume'];
$number_of_pages=$_POST['number_of_pages'];
$source=$_POST['source'];
$cost=$_POST['cost'];
$quantity=$_POST['quantity'];
$note=$_POST['note'];
$category=$_POST['category'];   

$sql=mysqli_query($con,"insert into add_book(purchase_date,assession_no,book_title,edition,publisher,year_of_publish,volume,number_of_pages,source,cost,quantity,note,category) values('$purchase_date','$assession_no','$book_title','$edition','$publisher','$year_of_publish','$volume','$number_of_pages','$source','$cost','$quantity','$note','$category')");

} ?>

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action=""  class="basic-grey" method="post" >
     <label>
        <span>Purchase Date:</span>
        <input name="purchase_date" type="text" class="datepicker" id="popup_container" required/>
    </label>
     <label>
        <span>Assession No. :</span>
        <input name="assession_no" type="text" value="" required/>
    </label>

    <label>
        <span>Book Title :</span>
        <input  type="text" name="book_title" id="title" placeholder="" value="" required/>

    </label>
     <label>
        <span>Edition :</span>
        <input name="edition" type="text" value="" required/>
    </label>

     <label>
        <span>Publisher :</span>
        <input name="publisher" type="text" value="" required/>
    </label>

   <label>
        <span>Year of Publish:</span>
        <input type="text"  name="year_of_publish" value="" required/>
    </label>

         <label>
        <span>Volume:</span>
        <input name="volume" type="text" value="" required/>
    </label>

    <label>
        <span>Page:</span>
        <input name="number_of_pages" type="text" value="" required/>
    </label>

    <label>
        <span>Source:</span>
        <input name="source" type="text" value="" required/>
    </label>
    <label>
        <span>Cost:</span>
        <input name="cost" type="text" value="" required/>
    </label>

      <label>
        <span>Quantity :</span>
        <input name="quantity" type="text" value="" required/>
    </label>

    <label>
        <span>General Note :</span>
        <textarea id="message" name="note" placeholder="" value=""></textarea>
    </label> 
     <label>
        <span>Category :</span><select name="category" required>
        <option value="Engineering">Engineering</option>
        <option value="General">General</option>
        <option value="Competitive">Competitive</option>
        <option value="Reading Space">Reading Space</option>
        </select>
    </label> 


                                                    <label>Image Upload</label>
                                                    <input type="file"  name="photo" /></span>
                                                                <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>                                                       





     <label>
        <span>&nbsp;</span> 
        <input type="submit" name="submit" class="button" /> 
    </label>    
</form>
</body>
</html>