?
Solved

PHP Edit Form Problem

Posted on 2011-10-24
4
Medium Priority
?
255 Views
Last Modified: 2012-05-12
I am creating an edit form in PHP and mysql. For some reason when I click on the link I have created on my display.php page, the edit page it is taking me to doesnt appear to exist (the website cannot be displayed message). The link is <a href='http://bu2003:83/php/systems/edit.php?id=$id'> and it is getting the correct ID and putting in the URL fine.
I have pasted the code for the edit page below. Connection file is good, and the sql queries are good, so i really cannt work out the problem. Any help would be appreciated.


editInfo.php
edit.php
0
Comment
Question by:booneball
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 37017125
This, from you edit.php, is wrong and I cannot tell what it is you are trying to do.


if ($count > 0)
{
         $row = mysql_fetch_array($sql)){ //Too many parentheses and no closing braces
         $id = $row["id"];


0
 

Author Comment

by:booneball
ID: 37017406
I fixed that error it was in the area you said. Only problem I am having with it now is that when i try updating changes in the edit form it tells me that the query is empty and consequently nothing happens.
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 37017838
I do not like the way you are doing your query. Try this. I have use an assoc instead of array just because I prefer them.

$sql = "SELECT * FROM it.system WHERE id='$id' LIMIT 1";

        $sql_result = mysql_query($sql);

        $row_num = mysql_num_rows($sql_result);

        for ($i = 0; $i < $row_num; $i++)
        {
            $row_out[$i] = mysql_fetch_assoc($sql_result);
        }

        foreach($row_out as $row)
        {
            $id = $row["id"];
            //etc.
            print_r($id); //To view the result
        }

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37023742
It is helpful to use the code snippet when posting code.  That way we see the line numbers and can read the code in clear text without having to download it.

Here is "edit."
<?php



include_once("conn.php");

	$id = $_GET['id'];  
	
	// Generate SQL Insert Query
	$sql = mysql_query("SELECT * FROM it.system WHERE id='$id' LIMIT 1");
	$result = mysql_query($sql);
	$count = mysql_num_rows($sql); // count the output amount
    if ($count > 0) {
		$row = mysql_fetch_array($sql)){ 
	
	
	 		 $id = $row["id"];
			 $client_name = $row["client_name"];
			 $supplier = $row["supplier"];
			 $purchased_date = $row["purchased_date"];
			 $warranty_expires = $row["warranty_expires"];
			 $company = $row["company"];
			 $department = $row["department"];
			 $os = $row["os"];
			 $os_serial = $row["os_serial"];
			 $ram = $row["ram"];
			 $processor = $row["processor"];
			 $office_version = $row["office_version"];
			 $office_serial = $row["office_serial"];
			 $additional_software = $row["additional_software"];
			 $user = $row["user"];
			 
	
	?>
    
    <h1 class="h1">Client Machines - Edit Information</h1>
        <form name="form1" action="http://bu2003:83/php/systems/editInfo.php" method="post"><br />

    <table width="90%" border="0" cellspacing="6">
      <tr>
        <td width="22%">Client_Name:</td>
        <td width="78%"><label>
          <input type="text" name="client_name" id="client_name" style="width:50%;" value="<?php echo $client_name; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Supplier:</td>
        <td><label>
          <input type="text" name="supplier" id="supplier" style="width:50%;" value="<?php echo $supplier; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Purchased Date:</td>
        <td><label>
          <input type="text" name="purchased_date" id="purchased_date" style="width:50%;" value="<?php echo $purchased_date; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Warranty Expires:</td>
        <td><label>
          <input type="text" name="warranty_expires" id="warranty_expires" style="width:50%;" value="<?php echo $warranty_expires; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Company:</td>
        <td><label>
          <select name="company" id="company" <?php echo $company; ?>>
            <option>Hayward Baker</option>
            <option>Victory Legal</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td>Department:</td>
        <td width="50%"><label>
          <input type="text" name="department" id="department" style="width:50%;" value="<?php echo $department; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Operating System:</td>
        <td><label>
          <select name="os" id="os"> 
            <option>Vista</option>
            <option>XP</option>
            <option>Windows 7</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td>Operating System Serial No:</td>
        <td><label>
          <input type="text" name="os_serial" id="os_serial" style="width:50%;" value="<?php echo $os_serial; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>RAM:</td>
        <td><label>
          <input type="text" name="ram" id="ram" style="width:50%;" value="<?php echo $ram; ?>"  />
        </label></td>
      </tr>
      <tr>
        <td>Processor:</td>
        <td><label>
          <input type="text" name="processor" id="processor" style="width:50%;" value="<?php echo $processor; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Office Version</td>
        <td><label>
          <select name="office_version" id="office_version" <?php echo $office_version; ?> >
            <option>2003</option>
            <option>2007</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td>Office Serial</td>
        <td><label>
          <input type="text" name="office_serial" id="office_serial" style="width:50%;" value="<?php echo $office_serial; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>Additional Software</td>
        <td><label>
          <input type="text" name="additional_software" id="additional_software" style="width:50%;" value="<?php echo $additional_software; ?>" />
        </label></td>
      </tr>
      <tr>
        <td>User</td>
        <td><label>
          <input type="text" name="user" id="user" style="width:50%;" value="<?php echo $user; ?>" />
        </label></td>
      </tr>
    </table>
    <input type="text" name="id" style="width:0; visibility:hidden;" value="<?php echo $id; ?>" />
  <input type="submit" value="Save Changes" style="width:140px;" />
  </form>
    <p>&nbsp;</p>
    
	</div>

Open in new window


Here is "editinfo."
<?php

include_once("conn.php");


			 
	$id = mysql_real_escape_string($_POST['id']);
    $client_name = mysql_real_escape_string($_POST['client_name']);
	$supplier = mysql_real_escape_string($_POST['supplier']);
	$purchased_date = mysql_real_escape_string($_POST['purchased_date']);
	$warranty_expires = mysql_real_escape_string($_POST['warranty_expires']);
	$company = mysql_real_escape_string($_POST['company']);
	 $department = mysql_real_escape_string($_POST['department']);
    $os = mysql_real_escape_string($_POST['os']);
	$os_serial = mysql_real_escape_string($_POST['os_serial']);
	$ram = mysql_real_escape_string($_POST['ram']);
	$processor = mysql_real_escape_string($_POST['processor']);
	$office_version = mysql_real_escape_string($_POST['office_version']);
	 $office_serial = mysql_real_escape_string($_POST['office_serial']);
    $additional_software = mysql_real_escape_string($_POST['additional_software']);
	$user = mysql_real_escape_string($_POST['user']);
	

	
	

	//Generate SQL Insert Query
	$sql = mysql_query("UPDATE it.system SET client_name='$client_name', supplier='$supplier', purchased_date='$purchased_date', warranty_expires='$warranty_expires', company='$company' department='$department', os='$os', os_serial='$os_serial', ram='$ram', processor='$processor' office_version='$office_version', office_serial='$office_serial', additional_software='$additional_software', user='$user', WHERE id='$id'");

	
	if (!mysql_query($sql,$conn))
  	{
  		die('Error: ' . mysql_error());
  	}
	echo "1 record ameneded";
	
	
?>

Open in new window


You might enjoy this little book.  It is not going to make anyone a professional, but it will help you get some foundation in PHP and MySQL, and will make some of the things like this question much easier for you.
http://www.sitepoint.com/books/phpmysql4/

You might also like this from the PHP web site about how to use HTML forms in PHP.
http://php.net/manual/en/tutorial.forms.php

Best of luck with your project, ~Ray
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question