Jump to content

PHP Updating A Database. Please Help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Aaron Lodge

Aaron Lodge

    Newbie

  • Members
  • Pip
  • 1 posts
Hey all! I'm currently working on a project consisting of a website and a database. I'm currently trying to allow a user to update data in the database. My code so far is only updating one same row every time I submit information. Maybe I have not structured it properly or maybe I'm missing something obvious; any code solution to my problem would be a great help as I'm still pretty much new to web development. My current code is below. I should add I'm using WAMP with Dreamweaver CS5 to code it. :c-smile:

<?php

session_start();

$hostname_logon = "127.0.0.1" ;   

$database_logon = "aal9" ;  

$username_logon = "root" ;  

$password_logon = "" ;


$coursenametext = '';

$id = '';

//error_reporting (E_ALL ^ E_NOTICE);


$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unable to connect to the database" );

mysql_select_db($database_logon) or die ( "Unable to select database!" );

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Edit Courses</title>

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

</head>

<div class="container">

<h1><a href="http://www.le.ac.uk"><img src="unilogo.gif" alt="Uol Logo" width="224" height="62" id="Uol_logo" align="left"/></a>Update Courses</h1>

</div>

<div class="sidebar1">

    <ul class="nav">

      <li><a href="homepage.php">Homepage</a></li>

      <li><a href="modules.php">My Modules</a></li>

      <li><a href="update.php">Update Database</a></li>

      <li><a href="googles1.php">Sem 1 Google</a></li>

      <li><a href="timetable2.php">Sem 2 Timetable</a></li>

      <li><a href="googles2.php">Sem 2 Google</a></li>

      <li><a href="logout.php">Logout</a></li>

    </ul>

    <p><a href="javascript:window.print()">Print This Page</a></p>

    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. </p>

    <p>Welcome To Uol Timetable Management. Please Find All Necessary Links Above.</p>

</div>

<div class="content">

<form action="editcourses.php" method="post">

<table border="1" cellspacing="1" align="center" bordercolor="#FFFFFF">

<tr>

<td>Course Name</td>

<td>Edit</td>

<td>Submit</td>

</tr>

<?php

$query = "SELECT * FROM Courses ";

$result = mysql_query ($query);

if(mysql_num_rows($result)>0){

                while($row = mysql_fetch_array($result)) {

                echo('<tr>' .

                '<td>' . $row['CourseName'] . '</td>' .

                '<td><input type="hidden" name="id" value="' . $row['ID'] . '"/><textarea name="coursenametext" rows="2"></textarea>' . '</td>' .

                '<td>' . '<input type="Submit" name="Submit" value="Submit">' . '</td>' .

                '</tr>');

        }

}

if(isset($_POST['Submit']))

{

$query1 = "UPDATE courses SET courseName = '".$_POST['coursenametext']."' WHERE ID = '".$_POST['id']."'";

$result1 = mysql_query ($query1);

}

?>


</table>    

</form>

<br>

</div>

<div class="footer">

<p><a href="mailto:aal9@le.ac.uk" class="Email">Click Here To Email For Help With Faults</a></p>

</div>

</div>

</body>

</html>



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Maybe I'm blind, but it looks like your form has no inputs, so the ID is always ''.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
grisha

grisha

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
It is generated in the PHP code (inside the loop).
The thing is that you have multiple hidden inputs inside a form named the same way (id). That's why no mater what row you edit, when you submit the form there will be only one value of 'id' in the $_POST table. Naming them id[] instead of id could help a bit if you would like to iterate through the whole $_POST['id'] table (using foreach) and then update reach row in your database.

Updating only selected rows is a bit more complicated ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users