dougai0138 2016-02-22 08:04
浏览 50
已采纳

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

I am creating an Invitation Card app for my upcoming event which will be held. My code successfully inserts the data into mysql database named booking having table name data. But there is problem with retrieving. When I fill the form and submit, it saves data in db but generates nothing. It gives following error:

Fatal error: Call to a member function query() on resource in C:\xampp\htdocs\booking\index.php on line 44

Here is my code, please tell me how to resolve this issue. I shall be very thankful to you.

<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条回答 默认 最新

  • douhui8163 2016-02-22 08:56
    关注

    Change

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

    To

    $result = mysql_query($sql);
    

    For more info click here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?