Page 1 of 1

Display database items to edit

Posted: Mon Jul 27, 2009 2:25 pm
by Kandersonnc
I'm trying to get my web page to show a record from a database and display the record data in
separate fields. I used to run this under Mandriva and it worked perfectly. I had since upgraded
the server to Red Hat and PHP 5.1.6. Now this results page no longer works. I get a page with all
my fields but there is no data in any of the fields. Here is the code below. The only thing I changed
from the original working file was I added this to the file '$_POST[select record]'"; otherwise I'd get
a "Unknown variable sel_record". Putting the post in front of this helped. So again, I'm trying to populate
fields with a database record for editing but all the fields are blank on the resulting page. Thanks for any
advice.

Ken

Code: Select all

<?php
    //File name: admin_addrecord1.php
    //Check to see if $PHP_AUTH_USER already contains info
    //Create connection
        $connection = mysql_connect("localhost","Anonymous","") or die
        ("Couldn't connect to database.");
 
        //Select database
        $db = mysql_select_db("Insurance", $connection) or die
        ("Couldn't select database.");   
        
    //SQL statement to select Vendor info<P><strong>OS:</strong>
    $sql = "SELECT * FROM Vendor WHERE Vend_num = '$_POST[sel_record]'"; 
 
    //Execute SQL query and get result
    $sql_result = mysql_query($sql,$connection) or die ("Couldn't execute query.");
 
        if (!$sql_result) {
            echo "<P>Couldn't get Vendor Info!";
        } else {
            //Fetch row and assign meaningful names to variables
            $row = mysql_fetch_array($sql_result);
            $Vend_num = $row["Vend_num"];
            $Vend_name = $row["Vend_name"];
            $Effective_date = $row["Effective_date"];
            $Expiration_date = $row["Expiration_date"];
            $General_Liability = $row["General_Liability"];
            $General_amount = $row["General_amount"];
            $Excess_amount = $row["Excess_amount"];
            $Additional_Insured = $row["Additional_Insured"];
            $pdf_link = $row["pdf_link"];
            $Company_num = $row["Company_num"];
            $Update_requested = $row["Update_requested"];           
 
 
 
    echo "
    <HTML>
    <HEAD>
    <TITLE>Modify Vendor Certificate of Liability Info</TITLE>
    </HEAD>
    <BODY>
    <h1>You have selected the following Vendor's Certificate of Liability info to modify</h1>
    <FORM method=\"POST\" action=\"admin_modrecord3.php\">
    <table cellspacing=2 cellpadding=2>
 
    <tr>
    <td valign=top><strong>Company #:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Company_num\" size=3 maxlength=3></td>
    </tr>
 
    <tr>
    <td valign=top><strong>Vendor #:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Vend_num\" size=10 maxlength=10></td>
    </tr>
    
    <tr>
    <td valign=top><strong>Vendor Name:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Vend_name\" size=75 maxlength=75></td>
    </tr>
 
    <tr>
    <td valign=top><strong>Effective Date:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Effective_date\" size=15 maxlength=15></td>
    </tr>
 
    <tr>
    <td valign=top><strong>Expiration Date:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Expiration_date\" size=15 maxlength=15></td>
    </tr>
 
    <tr>
    <td valign=top><strong>General Liability Type:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"General_Liability\" size=15 maxlength=15></td>
    </tr>
 
    <tr>
    <td valign=top><strong>General Amount:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"General_amount\" size=20 maxlength=20></td>
    </tr>
 
    <tr>
    <td valign=top><strong>Excess Amount:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Excess_amount\" size=20 maxlength=20></td>
    </tr>
 
    <tr>
    <td valign=top><strong>Additional Insured:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Additional_Insured\" size=3 maxlength=3></td>
    </tr>
 
    <tr>
    <td valign=top><strong>PDF Link:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"pdf_link\" size=95 maxlength=95></td>
    </tr>
    <tr>
    <td valign=top><strong>Update Reuested:</strong></td>
    <td valign=top><INPUT type=\"text\" name=\"Update_requested\" size=15 maxlength=15></td>
    </tr>
    <tr>
    <td align=center colspan=2><INPUT type=\"submit\" value=\"Modify Certificate Info\"></td>
    </tr>
    </table>
</FORM>
</BODY>
</HTML>
";
 
}
{
}
?>

Re: Display database items to edit

Posted: Mon Jul 27, 2009 3:12 pm
by califdon
I must assume that this isn't the entire script, as it doesn't make much sense the way it is. If you're not getting any data from your query, I'd suspect that you're not selecting any record. Why wouldn't you be selecting any record? Well, your SQL is looking for a record whose Vend_num value is equal to some value you are apparently expecting from a Form Post array. It would be a good idea to check to see whether that value is what you expect it to be. You are already using the PHP command echo, so I assume that you know how to use that to send a value to the browser. I suggest that you do that and see if that helps narrow it down.

Re: Display database items to edit

Posted: Mon Jul 27, 2009 3:43 pm
by califdon
Hah! I didn't even get that far down in his script. Good catch.

Re: Display database items to edit

Posted: Tue Jul 28, 2009 11:03 am
by Kandersonnc
Please edit your post to enclose code segments in

Code: Select all

tags. Thank you.[/color]

I thought I had copied this file from another script of mine that had worked before.
I don't what I did with the input type between editing.  Thanks that solved that problem.
I now have another problem. Once I submit that page's data using the web page's submit
button, it goes to display the next page which basically shows that the data was updated and
lists each field and it's contents and a link to get back to the main menu. This code below is
the code for that following page and it says unknown variable PC_serial on line 19. What I don't
get is this is an exact copy of this file off the old Mandriva machine running php version 4.3.1
and it worked perfectly. but now line 19 is an unknown variable. I'm only pasting in the first half
of the script as the rest of it is just the echo statement to display. I made the error line bold so
you can see where the error is happening.  Any ideas will be appreciated.


<?php
	//File name: admin_modrecord3.php
	//Check to see if $PHP_AUTH_USER already contains info
	if (!isset($_SERVER['PHP_AUTH_USER'] )){
		//If empty, send header causing dialog box to appear
		header('WWW-Authenticate: Basic realm=""');
		header('HTTP/1.0 401 Unauthorized');
		echo 'Authorization Required.';
		exit;

	} else if (isset($_SERVER['PHP_AUTH_USER'] )) {
		if (($_SERVER['PHP_AUTH_USER']!= "user") || ($_SERVER['PHP_AUTH_PW']!= "not real password")) {
		header('WWW-Authenticate: Basic realm="PWP PC Info Administration"');
		header('HTTP/1.0 401 Unauthorized');
		echo 'Authorization Required.';
		exit;

	} else 
		   [b] if (!$PC_serial) {[/b]
	    	header("Location http://pwpr.com/PC_admin/admin_modrecord1.php");
		exit;
	}

		//Create connection
		$connection = mysql_connect("localhost","Anonymous","") or die
		("Couldn't connect to database.");

		//Select database
		$db = mysql_select_db("Computers", $connection) or die
		("Couldn't select database.");


		//Prepare SQL Statment
		$sql = "UPDATE Hardware SET PC_serial = \"$PC_serial\", Branch = \"$Branch\", User = \"$User\", Department = \"$Department\", Net_Name = \"$Net_Name\", IP_Address = \"$IP_Address\", Make = \"$Make\", Model = \"$Model\", OS = \"$OS\", OS_Serial = \"$OS_Serial\", CPU = \"$CPU\", PC_serial_link = \"$PC_serial_link\", Antivirus = \"$Antivirus\", RAM = \"$RAM\", AV_ID = \"$AV_ID\" , Storage = \"$Storage\" , IP_outside = \"$IP_outside\" , service = \"$service\" WHERE PC_serial = \"$PC_serial\"";
		

		//execute SQL query and get result
		$sql_result = mysql_query($sql,$connection) or die
		("Couldn't execute query");

		if (!$sql_result) {
			echo "<P>Couldn't update record!";
		} else {

Re: Display database items to edit

Posted: Tue Jul 28, 2009 2:32 pm
by Kandersonnc
I tried both the $_GET['PC_serial'] and the $_POST['PC_serial'] you suggested but now I'm getting the following error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/html/intranet/PC_admin/admin_modrecord3.php on line 34

Line 34 is this

Code: Select all

$sql = "UPDATE Hardware SET PC_serial = $_GET['PC_serial'], Branch = $_GET['Branch'], User = $_GET['User'], Department = $_GET['Department'], Net_Name = $_GET['Net_Name'], IP_Address = $_GET['IP_Address'], Make = $_GET['Make'], Model = $_GET['Model'], OS = $_GET['OS'], OS_Serial = $_GET['OS_Serial'], CPU = $_GET['CPU'], PC_serial_link = $_GET['PC_serial_link'], Antivirus = $_GET['Antivirus'], RAM = $_GET['RAM'], AV_ID = $_GET['AV_ID'], Storage = $_GET['Storage'], IP_outside = $_GET['IP_outside'], service = $_GET['service'] WHERE PC_serial = $_GET['PC_serial']";
Any ideas? Again thank you for all your help.

Re: Display database items to edit

Posted: Tue Jul 28, 2009 3:49 pm
by Kandersonnc
I'm not sure I understand. My last entry was a sql statement to update a database.
I wasn't trying to display info using echo. Did I miss something?

Thanks

Ken