-5

I am really new to PHP and am messing around with adding and editing database values. I have accomplished adding information to the database, but I cannot seem to figure out how to edit it properly!

I created a file called edit.php and here is the code I have thus far:

<?php

include '../database/connection.php';

$id = $_GET['id'];
$first_name = $_GET['first_name'];
$last_name = $_GET['last_name'];
$university_id = $_GET['university_id'];

$sql = "UPDATE master_roster (first_name, last_name, university_id) VALUES ('$first_name', '$last_name', '$university_id') WHERE id = $id";

?>

No error messages of any help are posting. Whenever the form is submitted and is handed off to this file, I just get a blank screen with no results. I cannot seem to figure out what it is I am missing to have it update the content from input fields!

EDIT: I gave all of suggestions a shot but it still does not work! Here is the form that the data is coming from:

<?php 
  $id = $_GET['id'];
  $first_name = $_GET['first_name'];
  $last_name = $_GET['last_name'];
  $university_id = $_GET['university_id'];

?>
<form action="edit_member.php?id=<?php echo $id; echo "&first_name="; echo $first_name; echo "&last_name="; echo $last_name; echo "&university_id="; echo $university_id; ?>" method="post">
  <table>
    <tr>
      <td>First Name</td>
      <td><input type="text" name="first_name" value="<?php echo $first_name; ?>"></td> 
    </tr>
    <tr>
      <td>Last Name</td>
      <td><input type="text" name="last_name" value="<?php echo $last_name; ?>"></td> 
    </tr>
    <tr>
      <td>University ID</td>
      <td><input type="text" name="university_id" value="<?php echo $university_id; ?>"></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value="Submit"></td>
    </tr>
  </table>
</form>

I am not too concerned about SQL Injections at this point because I am just trying to learn the basics.


EDIT #2:

LIST.PHP - where the DB pulls all the members

<table>
  <tr>
    <th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>University ID</th>
  </tr>
  <?php
  include '../database/connection.php';
  $sql = "SELECT * FROM master_roster";
  if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['first_name'] . "</td>";
        echo "<td>" . $row['last_name'] . "</td>";
        echo "<td>" . $row['university_id'] . "</td>";
        echo "<td><a href='form.php?id=" . $row['id'] . "&first_name=" . $row['first_name'] . "&last_name=" . $row['last_name'] . "&university_id=" . $row['university_id'] . "'>Edit</a></td>";
        echo "</tr>"; 
      }
      echo "</table>";
      mysqli_free_result($result); 
    } else {
      echo "No records matching your query were found."; 
    }
  } else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
  }
  mysqli_close($link);
  ?>

EDIT.PHP

<?php

include '../database/connection.php';

$id = mysqli_real_escape_string($_POST['id']);
$first_name = mysqli_real_escape_string($_POST['first_name']);
$last_name = mysqli_real_escape_string($_POST['last_name']);
$university_id = mysqli_real_escape_string($_POST['university_id']);

$sql = "UPDATE master_roster 
SET 
    first_name = '$first_name', 
    last_name = '$last_name', 
    university_id = '$university_id'
WHERE 
    id = $id";
?>

FORM.PHP

<form action="edit.php" method="post">
  <table>
    <tr>
      <td>First Name</td>
      <td><input type="text" name="first_name"></td> 
    </tr>
    <tr>
      <td>Last Name</td>
      <td><input type="text" name="last_name"></td> 
    </tr>
    <tr>
      <td>University ID</td>
      <td><input type="text" name="university_id"></td>
    </tr>
    <tr>
      <td><input type="submit" value="Submit"></td>
    </tr>
  </table>
</form>
Fever
  • 11
  • 2

2 Answers2

4

This is the wrong syntax for update. update takes a series of column=value clauses:

UPDATE master_roster 
SET    first_name = '$first_name', 
       last_name = '$last_name', 
       university_id = '$university_id'
WHERE  id = $id

Mandatory comment:
Using variable substitutions in strings like that leaves your code vulnerable to SQL injection attacks. You should consider using a prepared statement instead.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    In all fairness to the other answer, [this comment](http://stackoverflow.com/questions/42458232/insert-query-in-php-not-working#comment72059456_42458330) and [this one](http://stackoverflow.com/questions/42458232/insert-query-in-php-not-working#comment72059498_42458330) also apply to this answer and should not be left out of the equation. – Funk Forty Niner Feb 25 '17 at 16:29
  • @Mureinik thanks for the help! I gave that a shot and put it inside $sql ="" but to no avail! – Fever Feb 25 '17 at 16:49
1

You're writing wrong UPDATE statement.

Try this

$sql = "UPDATE master_roster 
SET 
    first_name = '$first_name', 
    last_name = '$last_name', 
    university_id = '$university_id'
WHERE 
    id = $id";

Now execute the query using statement below.

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

$result will return true if the insertion is successful and false if not.

Use this to check if it is done or not.

if($result == false){
    die( "Connection Failed: ".$conn->error );
}

You should also be prevented from SQL injection. You can use mysqli_real_escape_string() method.

$first_name = $_GET['first_name']; $safe_first_name = mysqli_real_escape_string($conn, $first_name);

You can also use parameterized query.

Read this.


Now read the codes below modify your both pages like this.

Form

<form action="edit.php" method="post">
  <table>
    <tr>
      <td>First Name</td>
      <td><input type="text" name="first_name"></td> 
    </tr>
    <tr>
      <td>Last Name</td>
      <td><input type="text" name="last_name"></td> 
    </tr>
    <tr>
      <td>University ID</td>
      <td><input type="text" name="university_id"></td>
    </tr>
    <tr>
      <td><input type="submit" value="Submit"></td>
    </tr>
  </table>
</form>

This will update the data.

<?php

include '../database/connection.php';

$id = mysqli_real_escape_string($conn, $_POST['id']);
$first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
$last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
$university_id = mysqli_real_escape_string($conn, $_POST['university_id']);

$sql = "UPDATE master_roster 
SET 
    first_name = '$first_name', 
    last_name = '$last_name', 
    university_id = '$university_id'
WHERE 
    id = $id";

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

if($result == false){
    die( "Connection Failed: ".$conn->error );
}

?>
Community
  • 1
  • 1
Siraj Alam
  • 9,217
  • 9
  • 53
  • 65