Jump to content
Larry Ullman's Book Forums

Failure In Inserting Some Data In Mysql Via Php Script


Recommended Posts

I am learning PHP and MYSQL via above Larry's book. I am practing the register.php and its not inserting the firstname, lastname,email to MYSQL though it works with password and current time. Thereby my view_users.php page is also not working.So please can any one suggest me any solution. I have wrote the same syntax with no mistake from the book.

Link to comment
Share on other sites

Yeah i already read the debugging chapter with its steps for debugging.After facing this error i applied all the steps to find out the core source for problem. But i still got the same result. I test the queries in mysql client and its working perfectly. Besides i wonder why i am getting error though i done like copy paste of what you had wrote in the book.

Link to comment
Share on other sites

Yeah i already read the debugging chapter with its steps for debugging.After facing this error i applied all the steps to find out the core source for problem. But i still got the same result. I test the queries in mysql client and its working perfectly. Besides i wonder why i am getting error though i done like copy paste of what you had wrote in the book.

 

That's good information to know, then: the kind of information you should include when you're asking for help. You should also read the forum guidelines before posting again, so you can see what other information would be very beneficial to those of us trying to assist.

 

But what you're saying doesn't make sense. You're printing out the exact query being run by your PHP script, with the values in it? And you run this exact query in the mysql client and it works?

 

 

Link to comment
Share on other sites

Yeah i am sorry for my negligence in reading the guidelines prior to posting. Because this problem piss me off in my smooth learning of PHP via your wonderful book and so i posted instantly to get some helps. So here is the code for register.php and view_users.php as same as Larry's book. Have a look and please send me the suggestions. Thank you!!!

 

1)first one is the register.php;

 

<?php # register.php

 

$page_title = 'Register';

include('includes/header.html');

 

//Check if the form has been submitted:

if(isset($_POST['submitted'])){

 

$errors = array(); //Initialise the array

 

//Check for a first name:

if(empty($_POST['first_name'])){

$errors[] = 'You forgot to enter your first name';

}else{

$fn = trim($__POST['first_name']);

}

 

//Check for your last name

if(empty($_POST['last_name'])){

$errors[] = 'You forgot to enter your last name';

}else{

$ln = trim($__POST['last_name']);

}

 

//Check for the email address:

if(empty($_POST['email'])){

$errors[] = 'You forgot to enter your email';

}else{

$e = trim($__POST['email']);

}

 

//Check for the password and match against the confirmed password.

if(!empty($_POST['pass1'])){

if($_POST['pass1'] != $_POST['pass2']){

$errors[] = 'Your password did not match the confirmed password';

}else{

$p = trim($_POST['pass1']);

}

}else{

$errors[] = 'You forgot to enter your password';

}

 

if(empty($errors)){ //if everything is ok

 

//Register the user in the database

 

require_once('../mysqli_connect.php');//connect to db

 

//Make the query:

$q = "INSERT INTO users (first_name,last_name,email,pass,registered_date) VALUES

('$fn','$ln','$e',SHA1('$p'),NOW())";

$r = @mysqli_query($dbc,$q); //Run the query

 

if($r){ //if it ran ok

 

//print the message

echo'<h1> Thank you!</h1>

<p>You are now registered. In chapter 11 you will actually be able to log in!</p><p><br/></p>';

}else{// if it didn't run ok

 

//public message:

echo'<h1> System Error</h1>

<p class="errors"> You could not be registered due to a system error.

We apologise for any incovenience.</p>';

 

//Debugging message:

echo'<p>'.mysqli_error($dbc).'<br/><br/> Query: '.$q.'</p>';

 

} //End of if($r) IF.

 

mysqli_close($dbc); //Close the database connection

 

//Include the the footer and quit the script:

include('includes/footer.html');

exit();

 

}else{ //Report the erros

 

echo'<h1>Errors!</h1>

<p class="error"> The following error(s) occured:<br/>';

foreach($errors as $msg){ //Print the each errors;

echo"-$msg<br/>\n";

}

echo'</p><p>Please try again.</p><p><br/></p>';

 

} //End of the if(empty($errors) IF

 

}//End of the main submit conditional

 

?>

 

<h1>Register</h1>

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

 

<p>First Name:<input type="text" name="first_name" size="15" maxlength="20"

value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

 

<p>Last Name:<input type="text" name="last_name" size="15" maxlength="40"

value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

 

<p>Email Address:<input type="text" name="email" size="20" maxlength="80"

value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

 

<p>Password:<input type="password" name="pass1" size="10" maxlength="20"/></p>

 

<p>Confirm Password:<input type="password" name="pass2" size="10" maxlength="20"/></p>

 

<p><input type="submit" name="submit" value="register"/></p>

 

<input type ="hidden" name="submitted" value="TRUE"/>

</form>

<?php include('includes/footer.html');

?>

 

2)Second one is the view_users.php which shows some System errors and page is not displayed. i thought it has something to do with the problem in register.php

 

<?php # view_users.php

 

//This script retrieves all the records from the user table

$page_title = 'View the current users';

include('includes/header.html');

 

//page Header:

echo'<h1> Registered Users</h1>';

 

require_once('../mysqli_connect.php'); //connect to db

 

//Make the query

$q = "SELECT CONCAT(last_name,',',first_name)AS name, DATE_FORMAT(registered_date,

'%M %d,%Y') AS date FROM users ORDER BY registered_date ASC";

 

$r =@mysqli_query($dbc,$q);//Run the query

 

if($r){ //If it ran ok display the records

 

//Table header

echo'<table align="center" cellspacing="3" cellpadding="3" width ="75%">

 

<tr>

<td align ="left"><b> Name</b></td>

<td align = "left"><b>Date Registered</b></td></tr>';

 

//Fetch and print all the records

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){

echo'<tr>

<td align ="left">'. $row['name'].'</td>

<td align ="left">'.$row['date'].'</td></tr>';

}

echo'</table>'; //close the table

 

mysqli_free_result($r); Free up the resources

 

}else{ //if it didn't ran ok

 

//public Message:

echo'<p class="error"> The current users could not be retrieved. we apologise for the inconvience.</p>';

 

//Debugging Message:

echo'<p>'.mysqli_error($dbc).'<br/><br/> Query:'.$q.'</p>';

 

}//End of the if ($r) IF

 

mysqli_close($dbc); //close the database connection

 

include('includes/footer.html');

?>

Link to comment
Share on other sites

Hey Everyone...i really appreciate if you could suggest your solution for the following bug i encountered in the view_users.php page. I put the code above as you can see. The errors is as follow;

 

Internal Server Error

 

The server encountered an internal error or misconfiguration and was unable to complete your request.

 

Please contact the server administrator, admin@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

 

More information about this error may be available in the server error log.

Link to comment
Share on other sites

@HartleySan....I did as you had pointed out and i found the error is in the line require_once('../mysqli_connect.php'); //connect to db But i don't know how to solve ths issue. Can you suggest me any means to solve it.

Link to comment
Share on other sites

Well, that makes sense. One of two things is wrong:

 

1) Your path to the MySQL connect file is wrong.

 

2) The info for connecting to the database is wrong.

 

More than likely, #1 is wrong. That would more than likely be the cause of the error. Be sure to check your path again.

Link to comment
Share on other sites

HartleySan...I observed and tried a lot but it still the same. Because my database connection created in register.php is same as one in view_users.php. And i observed that the insertion of users via register.php is working and view_users.php is not working. So i wonder???

Link to comment
Share on other sites

Well, that's why #1 in my above post is probably the cause. The path to your connect file for this script (not the other scripts) is probably wrong.

 

Again, without knowing more about your specific environment, it's hard to comment. Keep in mind that the path to the file needs to be from the original script that was called, as that is treated as the current directory.

Link to comment
Share on other sites

@HartelySan..Thank you very much for your time and sincere guidance in solving my issue with php script. I am happy with your sincere response. I have tried my best to write the whole script again and then i checked server error log for the error. I am happy to say that i am able to figure out and solve the error. The main cause of error is my negligence in putting some back slashes in comments of the script. That is the reason for internal server error. Now its working and no worry. Thank you All!!!

Link to comment
Share on other sites

 Share

×
×
  • Create New...