Jump to content

Recommended Posts

hi again,

after I clicked a particular data to edit, it will bring me to a html which i created, UpdateRecordForm.html

In there, how do i retrieve the data from database so it will show the data in the textbox that i can edit it.

 

<!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>adding a new data record</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
	<label>Student Number:
		<input name="StudentNumber" type="text" id="StudentNumber" size="10" />
	</label>
</p>

<p>
	<label>First Name:
		<input name="FirstName" type="text" id="FirstName" size="20" />
	</label>
</p>

<p>
	<label>Last Name:
		<input name="LastName" type="text" id="LastName" size="20" />
	</label>
</p>

<p>
	<label>Email Address:
		<input name="EmailAddr" type="text" id="EmailAddr" size="50" />
	</label>
</p>

<p>
	<label>Telephone:
		<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" />
	</label>
</p>

<p>
	<input type="submit" name="submit" id="submit" value="Update Record" />
</p>

</form>
</body>
</html>

 

hi again,

after I clicked a particular data to edit, it will bring me to a html which i created, UpdateRecordForm.html

In there, how do i retrieve the data from database so it will show the data in the textbox that i can edit it.

 

<!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>adding a new data record</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
	<label>Student Number:
		<input name="StudentNumber" type="text" id="StudentNumber" size="10" />
	</label>
</p>

<p>
	<label>First Name:
		<input name="FirstName" type="text" id="FirstName" size="20" />
	</label>
</p>

<p>
	<label>Last Name:
		<input name="LastName" type="text" id="LastName" size="20" />
	</label>
</p>

<p>
	<label>Email Address:
		<input name="EmailAddr" type="text" id="EmailAddr" size="50" />
	</label>
</p>

<p>
	<label>Telephone:
		<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" />
	</label>
</p>

<p>
	<input type="submit" name="submit" id="submit" value="Update Record" />
</p>

</form>
</body>
</html>

 

You're using raw HTML there, absolutely no php either. You're a far ways off from being able to achieve this, you might want to look into some starting PHP/MySQL tutorials.

 

Edit: Just noticed it's action is a php file, it would be extremely helpful if we could get a look at that.

<!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>adding a new data record</title>
</head>

<body>

<?php

include_once "connect_to_mysql.php";				//your script to connect to your database
$sqlCommand = "SELECT student_number, first_name, last_name, email, phone FROM (your table name) WHERE student_number='$student_number' LIMIT 1";	//
$query = mysql_query($sqlCommand, $myConnection) or die (mysql_error()); 
while ($row = mysql_fetch_array($query)) { 
    $student_number = $row["student_number"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
$email = $row["email"];
$phone = $row["phone"];
} 
mysql_free_result($query);

?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
	<label>Student Number:
		<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $student_number ?>" />
	</label>
</p>

<p>
	<label>First Name:
		<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $first_name ?>" />
	</label>
</p>

<p>
	<label>Last Name:
		<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $last_name ?>" />
	</label>
</p>

<p>
	<label>Email Address:
		<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $email ?>" />
	</label>
</p>

<p>
	<label>Telephone:
		<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $phone ?>" />
	</label>
</p>

<p>
	<input type="submit" name="submit" id="submit" value="Update Record" />
</p>

</form>
</body>
</html>

 

If you haven't already done it, your processUpdateRecord.php script will need to update the row with student_number of (whatever student you're editing).

 

Denno

haha @zurev & wolf.

 

anyway, am using mySQL, phpMyAdmin? and i think my question is a little mislead of what i want.

I'll explain it again.

and yes, it's plain HTML and the submit button action calls to another php file.

 

now my question is, how do I display my data from a database(SQL) (how to link to my database?) and display it in the HTML textbox.

Basically, this HTML is to edit my data thats why I need to show my existing data and to modify the data and submit to a .php to amend my modified data to the database.

 

I'm still working on the processUpdateRecord.php but before that i need to finish this HTML form first. I can show u my processAddRecord.php though. Basically, i'll just tweak a few from this to work on update, like using SELECT then UPDATE.

<?php
//connect to the database
$database = 'CCM3413';
$conn = @new mysqli('localhost', 'root', '', $database);
if (mysqli_connect_errno() != 0)
{
	$errno = mysqli_connect_errno();
	$errmsg = mysqli_connect_error();
	echo "Connect Failed with: ($errno) $errmsg<br/>\n";
	exit;
}

$conn->query("SET NAMES 'utf8'");

//get values from HTML page
$StudentNumber = $_POST["StudentNumber"];
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$EmailAddr = $_POST["EmailAddr"];
$PhoneNumber = $_POST["PhoneNumber"];

//add a new record into the table StudentInfo
$query_str = "INSERT INTO StudentInfo (StudentNumber, FirstName, LastName, EmailAddr,
PhoneNumber) VALUES('$StudentNumber', '$FirstName', '$LastName', '$EmailAddr',
'$PhoneNumber')";
$result = @$conn->query($query_str);
if ($result === FALSE)
{
	echo "Connection Failed <br/>";
	$conn->close();
	exit;
}
else
{
echo "<p>Inserted a new data record successfully</p>";
echo "<p><a href='displaying.php'>Display the table content</a></p>";
echo "<p><a href='AddRecordForm.html'>Add another data record</a></p>";
}
?>

 

 

<!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>adding a new data record</title>
</head>

<body>

<?php

include_once "connect_to_mysql.php";				//your script to connect to your database
$sqlCommand = "SELECT student_number, first_name, last_name, email, phone FROM (your table name) WHERE student_number='$student_number' LIMIT 1";	//
$query = mysql_query($sqlCommand, $myConnection) or die (mysql_error()); 
while ($row = mysql_fetch_array($query)) { 
    $student_number = $row["student_number"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
$email = $row["email"];
$phone = $row["phone"];
} 
mysql_free_result($query);

?>
<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
	<label>Student Number:
		<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $student_number ?>" />
	</label>
</p>

<p>
	<label>First Name:
		<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $first_name ?>" />
	</label>
</p>

<p>
	<label>Last Name:
		<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $last_name ?>" />
	</label>
</p>

<p>
	<label>Email Address:
		<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $email ?>" />
	</label>
</p>

<p>
	<label>Telephone:
		<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $phone ?>" />
	</label>
</p>

<p>
	<input type="submit" name="submit" id="submit" value="Update Record" />
</p>

</form>
</body>
</html>

 

If you haven't already done it, your processUpdateRecord.php script will need to update the row with student_number of (whatever student you're editing).

 

Denno

 

Thanks, I'll try this now. :)

<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $first_name ?>" />

 

where $first_name is you replace that with whatever variable your database data is stored in. Do the same for the rest.

Tried.. I'm not sure if my variables $sn, $fn, $ln, $addr, $phone actually captures data from my database.

and the textbox doesn't show the data either. it just shows what's in the clause.

here's a screenshot

AOquD.png

 

Here's my edited code:

<!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>adding a new data record</title>
</head>

<body>
<?php
        include_once "connect.php";
        $sn = (String)$_GET['id'];
        $sqlcommand = "SELECT studentnumber, firstname, lastname, emailaddr, phonenumber FROM studentinfo WHERE studentnumber='$sn' LIMIT 1";
        $query = mysql_query($sqlcommand, $myconnection) or die(mysql_error());
        while ($row = mysql_fetch_array($query)){
              $sn    = $row["studentnumber"];
              $fn    = $row["lastname"};
              $ln    = $row["lastname"];
              $addr  = $row["emailaddr"];
              $phone = $row["phonenumber"]; }
        
        mysql_free_result($query);
?>

<form id="form1" name="form1" method="post" action="processUpdateRecord.php">
<p>
	<label>Student Number:
		<input name="StudentNumber" type="text" id="StudentNumber" size="10" value="<?php echo $studentnumber ?>" />
	</label>
</p>

<p>
	<label>First Name:
		<input name="FirstName" type="text" id="FirstName" size="20" value="<?php echo $fn ?>" />
	</label>
</p>

<p>
	<label>Last Name:
		<input name="LastName" type="text" id="LastName" size="20" value="<?php echo $ln ?>" />
	</label>
</p>

<p>
	<label>Email Address:
		<input name="EmailAddr" type="text" id="EmailAddr" size="50" value="<?php echo $addr ?>" />
	</label>
</p>

<p>
	<label>Telephone:
		<input name="PhoneNumber" type="text" id="PhoneNumber" size="20" value="<?php echo $phone ?>" />
	</label>
</p>

<p>
	<input type="submit" name="submit" id="submit" value="Update Record" value="" />
</p>

</form>
</body>
</html>

 

Help me please.. :(  I heard that we can't use php in a static HTML? is that right?

you need to save your file as a .php. That is currently saved as a .html. Saving it this way, it will see <?php and not know what the hell is going on with it.

 

So rename your file to be updaterecordform.php :).

 

Denno

Yes if you switch it to .php then it will work. Thanks for the help Denno I probably would have made a lot of mistakes due to lack of sleep. It's 12:30am I'm out for the night. Peace.

Okay can you do me a favor. Load up you web page and right click on a blank spot. Then go to see source code and post what it says.

 

Edit: Actually upload both .php files and I will send back a working copy(at least it should).

 

I can't test anything until either:

a. My webhost registers or

b. I get my localhost downloaded.

Well if that page is blank, then that just means there is errors in the php that need to be rectified.

 

So you're doing this for school work?

 

There is no other way that I know of which would allow you to edit a database without using php, and therefore a .php file extension..

 

Denno

OK, after renaming my file to .php, the page is totally blank.. lol  :'(

This is actually my tutorial, so my resources are limited. I'm only allowed to use HTML as a form then php for sql_query. something like that?

 

The form can't be in php anyway. It needs to be HTML. If the page is blank, make sure you have error reporting turned on. Output whatever you can.

 

"I heard that we can't use php in a static HTML? is that right?"

 

No, that's completely wrong. There is absolutely no other way to get a result from your database inserted into the 'value' of a form field without php (in this instance). This is probably the most common use for inline php.

Okay can you do me a favor. Load up you web page and right click on a blank spot. Then go to see source code and post what it says.

 

Edit: Actually upload both .php files and I will send back a working copy(at least it should).

 

I can't test anything until either:

a. My webhost registers or

b. I get my localhost downloaded.

 

k.. sounds good..

 

Well if that page is blank, then that just means there is errors in the php that need to be rectified.

 

So you're doing this for school work?

 

There is no other way that I know of which would allow you to edit a database without using php, and therefore a .php file extension..

 

Denno

It's my tutorial, just trying to learn php, sql, html etc..

 

 

[attachment deleted by admin]

Okay I have fixed the following errors in your code.

 

1. There was a table connection errror. That is why it would display, it was being killed by the connection.

2. I made your update record actually update instead of insert a new record.

3. I took out the lines that displayed Connected! when you connected to the database sucessfully.

4. I took out the line that randomly displayed the first name and last name twice.

5. I fixed your $fn variable to actually have $row['FirstName'] instead of $row['LastName'].

6. I fixed the CAPS on UpdateRecoredForm.php so it would actually go to it when you click edit.

7. You may have to check the processDeleteRecord.php in the display.php and make sure the CAPS MATCH.

 

And I think that is all of it.

 

[attachment deleted by admin]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.