Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

viewing db in php

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
I ahve just got the hang of using phpmyadmin to create tables and add records etc, Now im trying to add and view these records on a web page using php. But its not working :S here is the simple little code i am using:

<?php # script 7.3 - update.php



if (isset($_POST['submitted'])) {
$error = array();

if (empty($_POST['firstname'])) {
$error[] = 'You forgot to enter your Firstname';
} else {
$fn = trim($_POST['firstname']);
}

if (empty($_POST['surname'])) {
$error[] = 'You forgot to enter your Surname';
} else {
$sn = trim($_POST['surname']);
}

if (empty($error)) {

require_once ('mysql_connect.php');

$query = "INSERT INTO personel (firstname, surname)
VALUES ('$fn', '$sn', NOW() )";
$result = @mysql_query ($query);
if ($result) {
echo 'Database updated';
} else {
echo 'Error could not be updated';
exit();
}
mysql_close();
} else {
echo 'Error';
}
echo 'Please try again';
}
?>
<form method="post" action="update.php">
First name: <input name="firstname" type="text" value="">
<br>
Last name: <input name="lastname" type="text" value="">
<br>
<input type="submit" name="submit" value="Submit">
</form>

All it does is clear the fields in the form and does not add a record to the database! any ideas plz
 
If that's the complete script, you're missing the mysql_connect(). Remove the @ from the mysql_query call to see more of what's going on.
 
Yep they have been done as well as another spelling error i missed but still nothing happens! the form is just cleared and the database is not updated!

changes made:

$query = "INSERT INTO personel (firstname, surname, datefieldname)
VALUES ('$fn', '$sn', NOW() )";

and:

<form method="post" action="update.php">
First name: <input name="firstname" type="text" value="">
<br>
Last name: <input name="surname" type="text" value="">
<br>
<input type="submit" name="submit" value="Submit">
</form>
 
You're right, I missed the include statement when I read it.

You could also try echoing the $query to make sure it says what you think it should, and running the query manually as the user specified in mysql_connect.php.

What's that file look like, anyway?
 
I have specified the mysql_connect() in a different file which is called here:

require_once ('mysql_connect.php');
 
You might get PHP to tell you what is going wrong.

Change:

[tt] if ($result) {
echo 'Database updated';
} else {
echo 'Error could not be updated';
exit();
}[/tt]

for:

[tt] if ($result) {
echo 'Database updated';
} else {
echo 'Error could not be updated:'[red] . mysql_error()[/red];
exit();
}[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
It's really strange cause i can enter a name into the fields when i click submit the fields go blank and thats it lol. its as if there are no errors in the code but something is missing for it to do what it is supposed to! and i can't figure out what that is! im connecting the database otherwise i would get an error surely
 
Perhaps. Perhaps not.

If you have a SQL error on your insert, your code will not necessarily output some kind of error. Unless you explicitly tell PHP to tell you what is wrong, you may never know.

For example, the following code has an SQL error. The number of columns in the query doesn't match the number of values:

Code:
<?php

$dbh = mysql_connect ('localhost', 'test', 'test');

mysql_select_db ('test', $dbh);

$query = "INSERT INTO foo (constant_name) VALUES ('foo', 'bar')";

$return = mysql_query($query, $dbh);

if ($return)
{
	print 'success!';
}
else
{
	print 'problem!';
}
mysql_close ($dbh);
?>

All this code outputs is "problem!" and no record is added to the database.

But if I modify the code to produce more meaningful error messages:

Code:
<?php

$dbh = mysql_connect ('localhost', 'test', 'test');

mysql_select_db ('test', $dbh);

$query = "INSERT INTO foo (constant_name) VALUES ('foo', 'bar')";

$return = mysql_query($query, $dbh);

if ($return)
{
	print 'success!';
}
else
{
	print 'problem: ' . mysql_error();
}
mysql_close ($dbh);
?>

The code now outputs "problem: Column count doesn't match value count at row 1".

This gives me the necessary information to fix the problem.





Want the best answers? Ask the best questions! TANSTAAFL!
 
Also You Submit button from the form doesn't match the value you are testing for submission:

Code:
if (isset($_POST['[red]submitted[/red]'])) {...

yet your form has no value with that name:

<form method="post" action="update.php">  
    First name: <input name="firstname" type="text" value="">
    <br>
    Last name: <input name="lastname" type="text" value="">
    <br>  
    <input type="submit" name="[red]submit[red]" value="Submit">
</form>

Try changing this:
if (isset($_POST['[red]submitted[/red]'])) {...

to this:
if (isset($_POST['[red]submit[/red]'])) {...




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you everyone for your help. Its working now, I would of been stcuk without all of your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top