在此代码中从mysql数据库中获取数据问题

时间:2016-02-22 08:04:17

标签: php html mysql database web

我正在为即将举行的活动创建一个邀请卡应用程序。我的代码成功地将数据插入到具有表名数据的名为booking的mysql数据库中。但是检索存在问题。当我填写表单并提交时,它会将数据保存在db中但不生成任何内容。它给出了以下错误:

致命错误:在第44行的C:\ xampp \ htdocs \ booking \ index.php中调用资源上的成员函数query()

这是我的代码,请告诉我如何解决此问题。我将非常感谢你。

<html>
<body>
    <?php 
        if(isset($_POST['add'])){
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = '';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

            if(! $conn){
                die('Could not connect: ' . mysql_error());
            }

            if(! get_magic_quotes_gpc()){
                $emp_name = addslashes($_POST['emp_name']);
                $emp_fname = addslashes($_POST['emp_fname']);   
                $emp_cnic = addslashes($_POST['emp_cnic']); 
                $emp_address = addslashes($_POST['emp_address']);

            } else {

                $emp_name = addslashes($_POST['emp_name']);
                $emp_fname = addslashes($_POST['emp_fname']);   
                $emp_cnic = addslashes($_POST['emp_cnic']); 
                $emp_address = addslashes($_POST['emp_address']);
            }


            $sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress) " . 

"VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";

            mysql_select_db('booking');
            $retval = mysql_query($sql, $conn);

            if(! $retval) {die('Could not enter data: ' . mysql_error());}


?>

<table border=2>
    ~~~~~~Your Invitation Card~~~~~
    <tr><td>Your Name</td><td><?php
        $sql = "SELECT name FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your Father Name</td><td>
        $sql = "SELECT fname FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your CNIC Number</td><td>
        $sql = "SELECT cnic FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your Postal Address</td><td>
        $sql = "SELECT address FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this 

Invitation card to paticipate in the function. </td></tr><br>
</table>



<?php

            mysql_close($conn);

        } else {

        ?>
        <form method = "post" action = "<?php $_PHP_SELF ?>">
            Name: <input type="text" name="emp_name" id="emp_name"><br>
            Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>

            CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>

            Address: <input type="text" name="emp_address" id="emp_address"><br>

            <input type="submit" name="add" id="add" value="Submit">
<?php
}

?>

</body></html>

3 个答案:

答案 0 :(得分:1)

更改

<!DOCTYPE html>
<html lang="en">
<head>
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
	<script src="drag.js"></script>	
</head>
<body>
	<div class="container">
	<div class="row">
		<div class="col-md-3" id="1box">	
				<img id="img1" src="images/image1.jpg">
		</div>
		<div class="col-md-3" id="2box">
				<img id="img2" src="images/image2.jpg">
		</div>
		<div class="col-md-3" id="3box">
				<img id="img3" src="images/image4.jpg">
		</div>
		<div class="col-md-3" id="4box">
				<img id="img4" src="images/image5.jpg">
		</div>
	</div>
	</div>
	<div class="row">
		<center><div class="col-md-push-4 col-md-4 mainbox" ondrop="drop(event)" id="mainbox">	
				<img id="centerimg" src="images/center.png">
		</div></center>
	</div>
	
</body>
</html>

$result = $conn->query($sql);

了解更多信息click here

答案 1 :(得分:0)

我认为您应该使用mysql_query而不是$ conn-&gt; query

答案 2 :(得分:0)

我很瘦我在你的代码中发现了两个错误。

你应该使用

 mysql_query($sql,$conn);

而不是(之前提到的)

$result = $conn->query($sql);

你在html表格中错过了几个开放的php标签。

尝试以下代码,如果有效,请告诉我。                

        if(! $conn){
            die('Could not connect: ' . mysql_error());
        }

        if(! get_magic_quotes_gpc()){
            $emp_name = addslashes($_POST['emp_name']);
            $emp_fname = addslashes($_POST['emp_fname']);   
            $emp_cnic = addslashes($_POST['emp_cnic']); 
            $emp_address = addslashes($_POST['emp_address']);

        } else {

            $emp_name = addslashes($_POST['emp_name']);
            $emp_fname = addslashes($_POST['emp_fname']);   
            $emp_cnic = addslashes($_POST['emp_cnic']); 
            $emp_address = addslashes($_POST['emp_address']);
        }


        $sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress)   " . 

        "VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";

        mysql_select_db('booking');
        $retval = mysql_query($sql, $conn);

        if(! $retval) {die('Could not enter data: ' . mysql_error());}


        ?>

<table border=2>
 ~~~~~~Your Invitation Card~~~~~
<tr><td>Your Name</td><td><?php
    $sql = "SELECT name FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your Father Name</td><td>
  <?php
    $sql = "SELECT fname FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your CNIC Number</td><td>
  <?php
    $sql = "SELECT cnic FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your Postal Address</td><td>
  <?php
    $sql = "SELECT address FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this 

  Invitation card to paticipate in the function. </td></tr><br>
</table>



<?php

        mysql_close($conn);

    } else {

    ?>
    <form method = "post" action = "<?php $_PHP_SELF ?>">
        Name: <input type="text" name="emp_name" id="emp_name"><br>
        Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>

        CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>

        Address: <input type="text" name="emp_address" id="emp_address"><br>

        <input type="submit" name="add" id="add" value="Submit">
        <?php
      }         

      ?>

    </body></html>