0
    <?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
    or die ("could not connect: " . mysqli_error($conn));
    print "successful connection<br>";

    mysqli_select_db($conn, 'B00657633') or die ('db will not open');

    header('Location: sql5_2.php');

    $query = "UPDATE patient3 SET AGE=68 WHERE POST_CODE='bt667ed'";
    $result = mysqli_query($conn, $query) or die ("Query is invalid");
    $rownum = mysqli_num_rows($conn)
$col = mysqli_num_fields($conn);
echo "<table border='2'><tr><th>POST_CODE</th>";
for($i=0; $i<$rownum; $i++){
    $rows = mysqli_fetch_row($result);
    echo "<tr>";
    for($j=0; $j<$col; $j++){
        echo "<td>" . $rows[$j] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

$rownum = mysqli_affected_rows($conn);

echo $rownum . " Records have been updated.";

mysqli_close($conn);
?>

That is the first part of my web page, the second part is below:

<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
    or die ("could not connect: " . mysqli_error($conn));
    print "successful connection<br>";
        mysqli_select_db($conn, 'B00657633') or die ('db will not open');

    $query="SELECT * FROM patient3";
    $result = mysqli_query($conn, $query) or die ("Query is invalid");
    $num = mysqli_num_rows ($result);
    $col = mysqli_num_fields ($result);

    echo "<table border='2'><tr><th>PATNUM</th><th>PAT_FORENAME</th><th>PAT_SURNAME</th><th>STREET_ADDRESS</th><th>TOWN</th><th>POST_CODE</th><th>AGE</th>";
        for($j=0; $j<$num; $j++){
    $rows = mysqli_fetch_row($result);
    echo "<tr>";
    for($i=0; $i<$col; $i++){
        echo "<td>" . $rows[$i] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

echo " Records have been updated.";

mysqli_close($conn);
?>

No matter what I try it doesnt let me update any information in my sql. Really not sure where i have went wrong, please help me. The second part is displaying my table. Connection to the server is fine, however nothing changes in the table once i click refresh

4
  • Does the user have access to update the table? Commented Mar 3, 2016 at 21:15
  • 1
    You're assuming your queries are working. Have you enabled error reporting? Commented Mar 3, 2016 at 21:16
  • Also you are going to a different page? before running any queries Commented Mar 3, 2016 at 21:17
  • yes i have access to update the table. no i havent enabled error reporting, how do you do that? yea i am aware that im going to another page before queries are run Commented Mar 3, 2016 at 21:19

1 Answer 1

2

Edit: Now that I actually found the time to test/look at your code, I noticed a few things.

Here is the update file.

<?php

    $conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
        or die ("could not connect: " . mysqli_error($conn));
        print "successful connection<br>";

        mysqli_select_db($conn, 'B00657633') or die ('db will not open');



    $query = "UPDATE patient3 SET AGE=68 WHERE POST_CODE='bt667ed'";
    $result = mysqli_query($conn, $query) or die ("Query is invalid");

    $rownum = mysqli_affected_rows($conn);

    echo $rownum . " Records have been1 updated.";


    header('Location: sql5_2.php');

?>

Here is the sql5_2.php file.

<?php
$conn = mysqli_connect ("localhost", "B00657633", "Jpr3EjPw")
    or die ("could not connect: " . mysqli_error($conn));
    print "successful connection<br>";
        mysqli_select_db($conn, 'B00657633') or die ('db will not open');

    $query="SELECT * FROM patient3";
    $result = mysqli_query($conn, $query) or die ("Query is invalid");
    $num = mysqli_num_rows ($result);
    $col = mysqli_num_fields ($result);

    echo "<table border='2'><tr><th>PATNUM</th><th>PAT_FORENAME</th><th>PAT_SURNAME</th><th>STREET_ADDRESS</th><th>TOWN</th><th>POST_CODE</th><th>AGE</th>";
        for($j=0; $j<$num; $j++){
    $rows = mysqli_fetch_row($result);
    echo "<tr>";
    for($i=0; $i<$col; $i++){
        echo "<td>" . $rows[$i] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

echo " Records have been updated.";

mysqli_close($conn);
?>

In your code, you were doing a few things. First off, you were attempting to pass through the connection as a result set for the num_rows/num_fields methods. These are expecting the return values that actually contain data, like from a select query. The update query will return true or false, and how many rows it affected. So even after updating it to take the result variable, it was still attempting to pass through a boolean value to the methods.

You'll notice that I changed it to

mysqli_affected_rows()

which will check the number of rows affected for that mysqli_connect object that the query was executed through. From there, we can use that variable to output how many rows were affected.

Also, you were attempting to output the values of fields from an update query, which doesn't return the fields. Off of the top of my head, you would have to run a select query with the same parameters to get the set of data that will be changed, and then update that set. This way, you can use the set of data from the first select query to output which rows are being changed.

Keep in mind, that by using header('Location: ') you will redirect the user. So any output within the first file will not be seen as they are being sent to sql5_2.php.

Sign up to request clarification or add additional context in comments.

8 Comments

how do i change this then?
Check the second block of code that I edited in. What are the contents of your sql5_2.php file?
added the $result still no difference. the sql5_2.php file is the code below the first bit of coding
Don't forget to add the missing ;. Try to comment out the include line and then execute the update command. Will that update anything?
Also, is patient3 the name of your table?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.