Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

php - Can't output relevant mysql information to clicked link using SELECT *FROM table WHERE variable LIKE '$variable'

I have a mysql database named "drinks", in that database I have one table named "persons" and in "persons" I have two people, Bryan(fname) Fajardo(lname) 21(age) and Andross H Age:20.

In my index.php I have links set up from all of the people in table persons. I am trying to get my links to work so that when I click on either name, the information relevant from that person is outputted into my other page (where the link goes to) which is: insert.php.

I have been trying for hours to run some test by clicking on the Bryan link and outputting only his last name etc. etc. My objective: is to be able to link the people from "persons" table and then upon click go to insert.php and output that person's information there.

Here is my current code from Index.php.

<html>
<head>



<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
//Connect to the database
    $con = mysqli_connect("localhost","username","password","drinks");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }else{
        echo '<span style = "background: red ;">MSQL Connected.</span>' ;
    }

    $result = mysqli_query($con,"SELECT * FROM persons");

    while($row = mysqli_fetch_array($result)) {
    Print '<dd><a href=insert.php?
            fname="'.$row['fname'].'">'.$row['fname'].'</a></dd>';  
    }
    mysql_close();
    ?>


</body>
    </html>

and here is my Insert .php where I want the relevant information to be printed.

<html>
<head>



<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
<div class = "full-header">
    <div class = "mid-header span12 center">

    </div>
</div>
<div class = "main-content-container full_w">
    <div class = "span12 main-content center">
        <?php
        $cont = mysqli_connect("localhost","username","password","drinks");
        // Check connection
        if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }else{
            echo '<span style = "background: red ;">yay</span>' ;
        }
        $fname = $_GET['fname'];

        $sel = ($cont,"SELECT * FROM persons WHERE fname ='%$fname%'");
        while($rower = mysqli_fetch_array($sel)) {
            Print $rower['lname'];
                            //why is this not printing Bryan's last name?
        }
        ?>
    </div>
    </div>


</body>
</html>

Thank you in advance, I appreciate the help, I have just recently gotten into php and database building/summoning.

EDIT: I also have been reading that this is becoming deprecated and PDO is going to be used now, if you have a solution that involves PDO, I would appreciate that as well, but I am very new to PDO. EDIT 2: Changed "table" to "persons" in insert.php query.Still did not fix.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can understand how it is when first starting out. Once you wrap your mind around the basic parts of it the rest will flow.

Since you asked for a better way I am going to suggest a class I personally use in all my projects.

https://github.com/joshcam/PHP-MySQLi-Database-Class

Of course don't forget to download the simple MYSQLI class from the link above and include it just like I do below in your project. Otherwise none of this will work.

Here us the first page which contains the table with all the users from your persons Db table. We list them in a table with a simple edit/view button.

PAGE 1

 <?php 
        require_once('Mysqlidb.php');

        //After that, create a new instance of the class.

    $db = new Mysqlidb('host', 'username', 'password', 'databaseName');

    //a simple select statement to get all users in the DB table persons
    $users = $db->get('persons'); //contains an Array of all users 


    ?>
    <html>
    <head>



    <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>

<table>

    <th>
        First Name
    </th>
    <th>
        Last Name
    </th>
    <th>&nbsp;</th>

<?php 

//loops through each user in the persons DB table
//the id in the third <td> assumes you use id as the primary field of this DB table persons
foreach ($users as $user){ ?>
    <tr>
        <td>
            <?php echo $user['fname'];?>
        </td>
        <td>
            <?php echo $user['lname'];?>
        </td>
        <td>
        <a href="insert.php?id=<?php echo $user['id']; ?>"/>Edit/View</a>   
        </td>
    </tr>

<?php } ?>

</table>
</body>
    </html>

So that ends your first page. Now you need to include this code on your second page which we are assuming is called insert.php.

PAGE 2

<!--add this to your insert page-->

 <?php 
        require_once('Mysqlidb.php');

        //After that, create a new instance of the class.

    $db = new Mysqlidb('host', 'username', 'password', 'databaseName');

    //a simple select statement to get all the user where the GET 
    //variable equals their ID in the persons table
    //(the GET is the ?id=xxxx in the url link clicked)

    $db->where ("id", $_GET['id']);
    $user = $db->getOne('persons'); //contains an Array of the user

    ?>

<html>
<head>



<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <table>

<th>
    First Name
</th>
<th>
    Last Name
</th>
<th>user ID</th>


<tr>
    <td>
        <?php echo $user['fname'];?>
    </td>
    <td>
        <?php echo $user['lname'];?>
    </td>
    <td>
    <?php echo $user['id']; ?>  
    </td>
</tr>

</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...