Jump to content

Reading Tables Help!


hayes121

Recommended Posts

Hi guys i am new here and new to mysql, I am having some trouble reading data from my table. When ever i run my code i get the following error:

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/lab9.php on line 8

 

How can i fix this i have tried all that i know and nothing is working, here is the code i am using

Any help will be greatly appricated!

 

<html>
<head>
<title></title>
<?php
$con = mysqli_connect("localhost", "root", "", "testTable");
echo '<table border="1">'."\n";
$result = mysqli_query($con, "SELECT * FROM table");
while ( $row = mysqli_fetch_array($result))
    {
    echo("</tr><td>");
    echo(htmlentities($row[0]));
    echo("</td><td>");
    echo(htmlentities($row[1]));
    echo("</td><td>");
    echo(htmlentities($row[2]));
    echo("</td><td>");
    echo('<a href="edit.php?id=' .htmlentities($row[0]).' ">Edit</a> /');
    echo('<a href="delete.php?id=' .htmlentities($row[0]).' ">Delete</a> ');
    echo("</td></tr>\n");
    }
mysqli_close($con);    
    
?>

Link to comment
Share on other sites

Thanks for the feedback guys!!! When you say the 'named indexes' does that mean the column names that i have set in the table myself? Also another question with regards php and databases.

I want to add a login in function to the site, I have all the html code for the site done and the php login file, but do i need to combine the two files to a .html file or a .php file? Or do i leave them seperate and reference the .php login from the html?

Link to comment
Share on other sites

When you say the 'named indexes' does that mean the column names that i have set in the table myself?

Yes

 

 

I want to add a login in function to the site, I have all the html code for the site done and the php login file, but do i need to combine the two files to a .html file or a .php file? Or do i leave them seperate and reference the .php login from the html?

Typically you will want all of your pages to be PHP. Any page that is requested from the user would need to perform a check to see if the user is logged in. If yes, display the requested content. If no, redirect to the login page. If you have any pure HTML pages and the user can type in the URL to that page you can't restrict them via any login functionality. Well, technically you can, but it gets complicated and not worth discussing at this time.

Link to comment
Share on other sites

sorry for all the questions and thanks a million for the help!

 

Is there a way to convert html to php? or do i just need to change the tags?

 

for example i have attached my 'parts' page (.html) and then some simple php code to print out a table from my data base. Would you be able to join the two of these for me and re upload it?

Login.html

test.php

Link to comment
Share on other sites

Is there a way to convert html to php?

 

Change the file name from somefile.htm to somefile.php and it's a PHP file. Now, it will still just output whatever you had before and you would still need to implement some code to initiate the check for login though.

 

Besides those two files won't do anything just by putting them together. FYI: many of us regulars get a little peeved by people posting homework on here and trying to get people to do it for them. I find it unlikely your DB is named "Assignment" for any other reason.

 Would you be able to join the two of these for me and re upload it?

 

No. If you want someone to do this for you, look at posting in the freelance forum. Although I doubt anyone would be willing to do it for free.

Link to comment
Share on other sites

Last question I promise :P

 

I have set up a seperate table in my data base called 'my_build', I am trying to insert new data into this table .

I have written up what i though was the write code but i doesnt seem to be adding the data to the table, can you see any problems with the code?

 

<?php
$con = mysqli_connect("localhost", "root", "", "Computers");
if ( isset($_POST['serial_no']) && isset($_POST['description']) && isset($_POST['price'])
    && isset($_POST['fname']) && isset($_POST['lname']) )
    {
        $s = mysql_real_escape_string($_POST['serial']);
        $d = mysql_real_escape_string($_POST['description']);
        $p = mysql_real_escape_string($_POST['price']);
        //$sql = "INSERT INTO `my_build` (Serial_no, Description, Price)
        //VALUES ('$s', '$d', '$p')";
        echo "<pre>\n$sql\n</pre>\n";
        mysqli_query($con, "INSTER INTO `my_build` (serial_no, description, price) VALUES ('$s', '$d', '$p')");
        echo 'Success - <a href="home.php">Continue...</a>';
        return;
    }
mysqli_close($con);    
?>

<p>Add A New User</p>
<form method="post">
<p>Serial_no:
<input type="text" name="serial_no"></p>
<p>Description:
<input type="text" name="description"></p>
<p>Price:
<input type="text" name="price"></p>
<p><input type="submit" value="Add New"/>
<a href="home.php">Cancel</a></p>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.