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 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行