Jump to content

Php And Ajax Mysql Database Example


Recommended Posts

Hi,I've used w3schools a lot with very little problems to date, it's a great site to learn new stuff. I am having one problem with this tutorial. Here's a link to the tutorial;TutorialI have gotten all the code loaded up, created a replica of the database shown on my myphpadmin account (database name is tmalik2 and table is called ajax_demo) but I can't seem to link the two. On the HTML page I get the drop down box with the four names and the table showing the headers but once I select the name nothing shows up. I've modified the code to fit my details so it looks like this on the php page;

<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'ajax_demo', '*****');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("tmalik2", $con);$sql="SELECT * FROM user WHERE id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);?>

I'm not sure why the table doesn't update because as far as I can see the database should be connected since I don't get an error message.My myphpadmin table has the 5 fields listed in the table, nothing more or less. I tried adding a column for id but that didn't help.Thanks for your help!

Edited by diego
Link to post
Share on other sites

First, to check on errors, add this to the top of your script before you get $q:error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);What that does is redirect all errors to a file called "error.log" in the same directory as your script. It won't print any errors out (with AJAX it's typically better to log them instead of print them to output). So, after adding that run the script again and check to see if you have an error.log file, and if so, what it contains. To write your other errors to the log as well, to keep them all in the same place, you can replace this line:die('Could not connect: ' . mysql_error());with this:error_log('Could not connect: ' . mysql_error());exit();You can write anything you want to the error log using the error_log function, it's handy to use when debugging AJAX and you just want to print things out (but since it's AJAX, you wouldn't see it if it just printed, or it would interfere with your normal output).If you do that and still don't get any errors, use error_log to do some debugging to print what's going on. I would print your query out:

$sql="SELECT * FROM user WHERE id = '".$q."'";error_log('SQL: ' . $sql);

check for an error on the query:

$result = mysql_query($sql);if ($result === false)  error_log('Query error: ' . mysql_error());

print out how many records were found:

$result = mysql_query($sql);if ($result === false)  error_log('Query error: ' . mysql_error());else  error_log('Query returned ' . mysql_num_rows($result) . ' records');

and you can even print each row:

while($row = mysql_fetch_array($result)){  error_log(print_r($row, true));  ...

Link to post
Share on other sites

If you are confident that the Database name and user name are correct, contact your Hosting Service to ask what should be used instead of 'localhost'.Also, some shared hosts require the account name as a prefix for the databse (ie: jlhaslip_dbname). Check that as well.

Link to post
Share on other sites

Thanks for your help. I'll give both ideas a shot. I don't know if this makes a difference but I'm using my university server and myphpadmin facilities so could that have an influence in why it won't connect?Thanks.Edit - I tried your suggestion and it seemed to have done something. Instead of having nothing show up the following is listed

Could not connect: Access denied for user 'tmalik2_ajax_dem'@'localhost' (using password: YES)
My username is wrong so changed back the code to the following;
$con = mysql_connect('localhost', 'tmalik2', '*******');...mysql_select_db("ajax_demo", $con);
Everything else is the same. Is the above correct with the first line having my username and password and the line below containing the table name in the database? If so I'm not sure what the error is since the table is created in myPhpAdmin.All I can think of is there may have been a fault with the way I've created my database whne trying to replicate the one used in the example. This is because I'm not getting any error messages at all which I would've gotten if the coding was wrong. It connects to the database but it just wont bring up the data when I select a name from the drop down box. Here's a screenshot of my database in myphpadmin.testszq.jpg Any help is really appreciated. Edited by diego
Link to post
Share on other sites

Thanks for the reply. I've amended the above code so it's as follows;

$con = mysql_connect('localhost', 'tmalik2', '*******');...mysql_select_db("tmalik2", $con);

I'm still getting the same problem. When the page loads up it tells me "user info will appear here" but when a name is selected from the drop down box all that shows up is the table headers. The only thing I can think of is my username and database name being the same? Can that be a problem? If not any other suggestions?Thanks.Edit: This problem seems to be related to just this specific area because I tried another php system to test if the database works and with the following code the database shows up perfectly fine?

<html><head><title>MySQL Table Viewer</title></head><body><?php$db_host = 'localhost';$db_user = 'tmalik2';$db_pwd = '*****';$database = 'tmalik2';$table = 'ajax_demo';if (!mysql_connect($db_host, $db_user, $db_pwd))	die("Can't connect to database");if (!mysql_select_db($database))	die("Can't select database");// sending query$result = mysql_query("SELECT * FROM {$table}");if (!$result) {	die("Query to show fields from table failed");}$fields_num = mysql_num_fields($result);echo "<h1>Table: {$table}</h1>";echo "<table border='1'><tr>";// printing table headersfor($i=0; $i<$fields_num; $i++){	$field = mysql_fetch_field($result);	echo "<td>{$field->name}</td>";}echo "</tr>\n";// printing table rowswhile($row = mysql_fetch_row($result)){	echo "<tr>";	// $row is array... foreach( .. ) puts every element	// of $row to $cell variable	foreach($row as $cell)		echo "<td>$cell</td>";	echo "</tr>\n";}mysql_free_result($result);?></body></html>

This is the exact same information entered into the previous system where I can't get it to show up. Is it possible I've gotten an error in maybe the javascript code or maybe created the database incorrectly? The JS code is below;

var xmlHttpfunction showHint(str){if (str.length==0)  {   document.getElementById("txtHint").innerHTML="";  return;  }xmlHttp=GetXmlHttpObject();if (xmlHttp==null)  {  alert ("Your browser does not support AJAX!");  return;  } var url="gethint.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);} function stateChanged() { if (xmlHttp.readyState==4){ document.getElementById("txtHint").innerHTML=xmlHttp.responseText;}}function GetXmlHttpObject(){var xmlHttp=null;try  {  // Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  }catch (e)  {  // Internet Explorer  try	{	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}  catch (e)	{	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}  }return xmlHttp;}

Edited by diego
Link to post
Share on other sites

I'm not real clear where you think the problem is. First, if you're debugging PHP, either use error logging or don't run it through AJAX, run it through a regular form. Once you know the PHP code is working, then you can use AJAX to get the response and deal with it. Similarly, use some test AJAX code with a static page instead of a PHP script. Since you know the information AJAX is getting back, then you can test to make sure the Javascript portion is working correctly.

Link to post
Share on other sites

Hi,I'm not sure what the problem is so I'm not sure where to look. I don't get any type of error message, it's just a case of the data not showing up when it's supposed to.I thought since the database connected fine using other coding maybe I've got the error in the javascript file as in the database I created has a small error that doesn't make it identical to the one needed or maybe in the php page where the mysql command SELECTS id isn't correct? I know it sounds confusing but I'm new to this so not really sure how to go about fixing this.

Link to post
Share on other sites

Hi again,I messed around with the code and have fixed the problem. The SELECT command was trying to get the information out of the table 'user' when that didn't exist. I named my table ajax_demo so linked it to that and it worked fine. Such a basic error but took me ages to realise!! I do have a couple of questions relating to this.Is there any way I can add in an option to show all the data in the database into the HTML table? or another way to just get all the information to show up before I get to selecting a user?Secondly, I'm trying to make my HTML table look and act like a spreadsheet. To start making it look like a spreadsheet I'm hoping to add in the basic features of adding, deleting and modifying the entries on the web page. What would be the best way to go about this?Thanks.

Link to post
Share on other sites
Is there any way I can add in an option to show all the data in the database into the HTML table?
Of course. The PHP script is only getting 1 record from the database, you can change that to get all records if you tell it to get all of them. In order to tell it, you can use a link or a button or whatever you want to use, it's still just using Javascript to send a request and handling it with PHP. The basic concept is the same, you would just have Javascript send other information besides the search string.
or another way to just get all the information to show up before I get to selecting a user?
You can just have a PHP script to output the records in the database, it doesn't need to come through AJAX. The PHP script right now is producing partial HTML, you can change it to produce the entire page.
Secondly, I'm trying to make my HTML table look and act like a spreadsheet. To start making it look like a spreadsheet I'm hoping to add in the basic features of adding, deleting and modifying the entries on the web page. What would be the best way to go about this?
Use a Javascript framework like ExtJS, it already has all of that built-in. You'll need to learn how to use ExtJS, but that's probably easier than learning everything that's required to do that on your own.http://extjs.com/deploy/dev/examples/grid/edit-grid.htmlhttp://extjs.com/deploy/dev/examples/grid-...rid-filter.htmlhttp://extjs.com/deploy/dev/examples/grid/grid3.html Edited by justsomeguy
Link to post
Share on other sites
Of course. The PHP script is only getting 1 record from the database, you can change that to get all records if you tell it to get all of them. In order to tell it, you can use a link or a button or whatever you want to use, it's still just using Javascript to send a request and handling it with PHP. The basic concept is the same, you would just have Javascript send other information besides the search string.
or another way to just get all the information to show up before I get to selecting a user?
You can just have a PHP script to output the records in the database, it doesn't need to come through AJAX. The PHP script right now is producing partial HTML, you can change it to produce the entire page.
Thanks for the reply. I now understand the theory behind this as in change it so instead of bringing about the single entry I can bring them all. What I'm not sure is how to go about that. Currently I'm using the ID to bring a seperate entry but there isn't anything in the table that I could bring every entry up? What kind of code will be needed for this? What I'm hoping to achieve is have the page load up with all the entries at the start and then I can go through the drop down box to limit the search.
Use a Javascript framework like ExtJS, it already has all of that built-in. You'll need to learn how to use ExtJS, but that's probably easier than learning everything that's required to do that on your own.http://extjs.com/deploy/dev/examples/grid/edit-grid.htmlhttp://extjs.com/deploy/dev/examples/grid-...rid-filter.htmlhttp://extjs.com/deploy/dev/examples/grid/grid3.html
I've been reading through a load of tutorials all day and I can't seem to get my head around it at all. I think it may be to complex for someone with as little knowledge of coding. Is there any simpler tutorials out there for just creating an editble HTML table?Thanks for your help, it is really appreciated!
Link to post
Share on other sites

If you're using this to get records based on the search string:$sql="SELECT * FROM user WHERE id = '".$q."'";Then this will get all records:$sql="SELECT * FROM user";

Is there any simpler tutorials out there for just creating an editble HTML table?
Not that I know of, the problem is that creating an editable table is not a trivial thing to do, there are a lot of pieces involved.
Link to post
Share on other sites
If you're using this to get records based on the search string:$sql="SELECT * FROM user WHERE id = '".$q."'";Then this will get all records:$sql="SELECT * FROM user";
Where would you suggest putting the code in? I tried replacing the code I've gotten with the new one and nothing showed up. Is there a way I can get the full table to show up instead of the "user info will be listed here". My full code is at the bottom of the post.
Not that I know of, the problem is that creating an editable table is not a trivial thing to do, there are a lot of pieces involved.
How about a feature to add an entry through the HTML page that will update the database? And similarly remove the data? Is there a tutotiral or something for that? Thanks.
<?phperror_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);$q=$_GET["q"];$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik WHERE id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);?>

var xmlHttpfunction showHint(str){if (str.length==0)  {   document.getElementById("txtHint").innerHTML="";  return;  }xmlHttp=GetXmlHttpObject();if (xmlHttp==null)  {  alert ("Your browser does not support AJAX!");  return;  } var url="gethint.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);} function stateChanged() { if (xmlHttp.readyState==4){ document.getElementById("txtHint").innerHTML=xmlHttp.responseText;}}function GetXmlHttpObject(){var xmlHttp=null;try  {  // Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  }catch (e)  {  // Internet Explorer  try	{	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}  catch (e)	{	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}  }return xmlHttp;}

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script></head><body background="background.PNG"><h2 align="center"><b><u><font size="8" face="Georgia, Arial" font color="blue">Malik's Gym</font></b></u></h2><form> <font color="blue"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Peter Griffin</option><option value="2">Lois Griffin</option><option value="3">Glenn Quagmire</option><option value="4">Joseph Swanson</option></select></form><p><center><div id="txtHint"><b>User info will be listed here.</b></div></center></p></body></html>

Thanks for any help!

Link to post
Share on other sites

You can add PHP code to the HTML page to print whatever you want there.

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script></head><body background="background.PNG"><h2 align="center"><b><u><font size="8" face="Georgia, Arial" font color="blue">Malik's Gym</font></b></u></h2><form> <font color="blue"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Peter Griffin</option><option value="2">Lois Griffin</option><option value="3">Glenn Quagmire</option><option value="4">Joseph Swanson</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th><th>Age</th><th>Hometown</th><th>Job</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>" . $row['FirstName'] . "</td>";echo "<td>" . $row['LastName'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Hometown'] . "</td>";echo "<td>" . $row['Job'] . "</td>";echo "</tr>";}echo "</table>";?></div></center></p></body></html>

How about a feature to add an entry through the HTML page that will update the database? And similarly remove the data? Is there a tutotiral or something for that? Thanks.
That's just basic form handling and database work. Read up on how to use PHP to process forms, there's a thread about that here:http://w3schools.invisionzone.com/index.php?showtopic=12509The w3schools site also has information, and there's information on php.net and online in general. Other than form processing, read about SQL and how to use it to add or remove data in a database. Learn how to process forms and work with the database, then you can adapt that code to use AJAX if you want to.
Link to post
Share on other sites

Thanks for the help. I used the code you provided and edited it to match my system but I get the following error?

Name ###### Age Address Post Code Contact Number Member Type "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['Name'] . ""; echo "" . $row['######'] . ""; echo "" . $row['Age'] . ""; echo "" . $row['Address'] . ""; echo "" . $row['Post_Code'] . ""; echo "" . $row['Contact_Number'] . ""; echo "" . $row['Member_Type'] . ""; echo ""; } echo ""; ?>
Here's the code;
<html><head><title>Malik's Gym</title><script src="selectuser.js"></script></head><body background="background.PNG"><h2 align="center"><b><u><font size="8" face="Georgia, Arial" font color="blue">Malik's Gym</font></b></u></h2><form> <font color="blue"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Tahir Malik</option><option value="2">Khalid Mahmood</option><option value="3">Amanda Murphy</option><option value="4">Pep Messoud</option><option value="5">Nabeela Din</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', 'England');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo "<table border='1'><tr><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['######'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['Post_Code'] . "</td>"; echo "<td>" . $row['Contact_Number'] . "</td>"; echo "<td>" . $row['Member_Type'] . "</td>"; echo "</tr>"; }echo "</table>";?></div></center></p></body></html>

The error quoted above just appears where the phrase "user info will appear" used to be. I've tried out the code as it's own file and it works fine. I've tried a few other ways using w3school tutorials to show the table and when I add the table as a seperate php file it works fine but when I try adding it the way you've put it in the error quoted above always appears. Any ideas?

That's just basic form handling and database work. Read up on how to use PHP to process forms, there's a thread about that here:http://w3schools.invisionzone.com/index.php?showtopic=12509The w3schools site also has information, and there's information on php.net and online in general. Other than form processing, read about SQL and how to use it to add or remove data in a database. Learn how to process forms and work with the database, then you can adapt that code to use AJAX if you want to.
Thanks/ I'll get cracking on that straight away. Edited by diego
Link to post
Share on other sites
I used the code you provided and edited it to match my system but I get the following error?
That's not an error, it's just printing the PHP code. Make sure your file is a .php file and not .html, and that you're running it through a web server.
Link to post
Share on other sites

Yep, that was the problem. Works fine now. Now I've just got to make this table as spreadsheet like as possible. Thanks for all your help so far, I'm going to move onto looking at the stuff you told me about and hopefully if I run into some problems I can get some help here.Thanks again!

Link to post
Share on other sites

Hi again,I used the POST variable and created an add button so that aspect of the database is working fine. The problem I'm having now is creating a delete feature. At the moment its just a html table with an add button at the bottom. What I want is something like the following;http://viralpatel.net/blogs/2009/03/dynami...javascript.htmlI've tried adapting the code there to fit my coding but can't seem to do it. What would be the best way to go about doing such a thing and are there any tutorials that I can follow?I know how to create a delete button that will delete a specific row, need one that deletes a row that the user chooses to on the HTML page.Thanks for any help!

Link to post
Share on other sites
I know how to create a delete button that will delete a specific row, need one that deletes a row that the user chooses to on the HTML page.
Isn't that the same thing? It looks like the code on the page you linked to is using checkboxes to identify which rows to delete. Another common way is to print a delete link for each row then you just click the link for the row you want to delete. All you need to tell PHP is the ID of the row you want to delete, so you'll need a way to save the ID. For a regular link, you would just put the ID in the link:<a href="delete.php?id=10">The example code you linked to doesn't do any database work, so it doesn't do anything with IDs, it just deletes whichever row in the table the checkbox is in. The Javascript code you're using above and the PHP code that produce it also don't have any IDs. You're going to need a way to get the ID to Javascript so that Javascript can send the ID back to PHP to tell it which record to delete. One example would be to have PHP print a delete link like this:
echo "<tr>";echo '<td><a href="javascript:void(0);" onclick="delete_record(' . $row['id'] . ');">Delete</a></td>';echo "<td>" . $row['FirstName'] . "</td>";echo "<td>" . $row['LastName'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Hometown'] . "</td>";echo "<td>" . $row['Job'] . "</td>";echo "</tr>";

So that has a call to a Javascript function called delete_record, and it sends it the ID of the record to delete. The delete_record function can get that ID and send it to PHP to do the delete in the database.

Edited by justsomeguy
Link to post
Share on other sites
I know how to create a delete button that will delete a specific row, need one that deletes a row that the user chooses to on the HTML page.
Isn't that the same thing? It looks like the code on the page you linked to is using checkboxes to identify which rows to delete. Another common way is to print a delete link for each row then you just click the link for the row you want to delete. All you need to tell PHP is the ID of the row you want to delete, so you'll need a way to save the ID. For a regular link, you would just put the ID in the link:<a href="delete.php?id=10">
I meant a specific entry not row so like the one listed above. Main reason I didn't wanna do this is because after I've added an entry the database updates straight away and when I return to the page the new entry will be there but I don't think there's a way for a whole new delete button to be created for the new entry to appear automatically as you add records?
The example code you linked to doesn't do any database work, so it doesn't do anything with IDs, it just deletes whichever row in the table the checkbox is in. The Javascript code you're using above and the PHP code that produce it also don't have any IDs. You're going to need a way to get the ID to Javascript so that Javascript can send the ID back to PHP to tell it which record to delete. One example would be to have PHP print a delete link like this:CODEecho "<tr>";echo '<td><a href="java script:void(0);" onclick="delete_record(' . $row['id'] . ');">Delete</a></td>';echo "<td>" . $row['FirstName'] . "</td>";echo "<td>" . $row['LastName'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Hometown'] . "</td>";echo "<td>" . $row['Job'] . "</td>";echo "</tr>";So that has a call to a Javascript function called delete_record, and it sends it the ID of the record to delete. The delete_record function can get that ID and send it to PHP to do the delete in the database.
My javascript knowledge is very basic so I hope I'm getting this right. the javascript function delete_record should be created in a seperate JS file but what would that file entail?Then I would create a php page just like the add function and create the above delete feature next to each entry and have that linked up so it would remove entries from the database and the html table?Also, would this above have a new delete button create for new entries added after I finish coding? If not how do I go about this?Thank you so much for the help, without your input I wouldn't be getting anywhere here!!EDIT:I've been trying to create a sort function for the table but I can't seem to get it working at all. I've ran through like 4-5 test tables and they all work fine with the test data but in my table I can't seem to do it. The code from the table is as follows;
echo "<table border='1'><tr><th>ID</th><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";
Now each sort function that I used (js files) needed me to add the script in the head and add something in the bolded area above. As soon as I add anything into that area my screen shows up blank. What am I doing wrong? Edited by diego
Link to post
Share on other sites
the javascript function delete_record should be created in a seperate JS file but what would that file entail?
It doesn't necessarily need to be a separate file, but I was thinking it would send an AJAX request like you're already doing to tell the PHP to delete a record. It would also be possible to send the function the row in the table you want to delete. If you just delete the record using AJAX, it won't update the HTML automatically until you refreshed the page, if you want to remove the row from the HTML also that's a little extra.
Also, would this above have a new delete button create for new entries added after I finish coding?
The PHP code that prints the rows prints the same thing for every row, it doesn't matter when you add each row.
As soon as I add anything into that area my screen shows up blank. What am I doing wrong?
I would have to see what you're trying to add. The quotes might not be matched correctly.
Link to post
Share on other sites

Here's my code;

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script><link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" /><script type="text/javascript" src="tablecloth/tablecloth.js"></script></head><body background="backgrounda.PNG"><h2 align="center"><b><u><font size="8" face="COMIC SANS MS" font color="#38ACEC">Malik's Gym</font></b></u></h2><form><font color="#38ACEC"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Tahir Malik</option><option value="2">Khalid Mahmood</option><option value="3">Amanda Murphy</option><option value="4">Pep Messoud</option><option value="5">Nabeela Din</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo <table><tr><th>ID</th><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['ID']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Name']. ');">Delete</a></td>';;echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['######']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Age']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Address']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Post_Code']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Contact_Number']. ');">Delete</a></td>';echo "<td>" . <a href="java script:void(0);" onclick="delete_record(' . $row['Member_Type']. ');">Delete</a></td>';echo "</tr>";}echo "</table>";?></div></center></p><center><FORM METHOD="LINK" ACTION="http://samp.inf.brad.ac.uk:59237/add.php"><INPUT TYPE="submit" VALUE="Add Member"></FORM></center></body></html>

From what I can see there's no 'delete_record' to call so is that why nothing is showing up? The screen is blank when I load it up. It's a .php file. Am I right in thinking I'll have to create a delete_record.js file that carries out the deleting? I have very little knowledge in this so is there a script I can get or an example bit of code? If this isn't the problem what is?Thanks for your help!!

Link to post
Share on other sites

There's no quote here:echo <table>If you're just seeing a blank page, that means error reporting is off, because that's a PHP error. You'll probably want to enable error reporting for the future:<?phperror_reporting(E_ALL);ini_set('display_errors', 1);?>That won't cause a syntax error to show up, but if you load a file and it's blank you can create another file to turn on error reporting first, then include the other file with the problem:<?phperror_reporting(E_ALL);ini_set('display_errors', 1);include 'file.php';?>It's not an error if you don't have a Javascript function defined unless you actually try to run it. It would be an error if you clicked on a delete link, but not if you just pull up the page.

Am I right in thinking I'll have to create a delete_record.js file that carries out the deleting
You don't need a file with a specific name, you don't even need an external Javascript file at all, you just need to define the delete_record function. It doesn't matter where you define it as long as it's defined. You've already got the showHint function that sends an AJAX request. You need the delete_record function to send the ID to delete to your PHP script which does the actual delete. When the response comes back you can refresh the page, or remove the row from the table, or whatever you want to do.
function delete_record(id){  xmlHttp=GetXmlHttpObject();  if (xmlHttp==null)  {	alert ("Your browser does not support AJAX!");	return;  }   var url="delete.php?id=" + id;  xmlHttp.onreadystatechange=function ()  {	// check for response, delete row from html table, etc  };  xmlHttp.open("GET",url,true);  xmlHttp.send(null);}

Link to post
Share on other sites
There's no quote here:echo <table>
I've ammended that in the code.
If you're just seeing a blank page, that means error reporting is off, because that's a PHP error. You'll probably want to enable error reporting for the future:<?phperror_reporting(E_ALL);ini_set('display_errors', 1);?>That won't cause a syntax error to show up, but if you load a file and it's blank you can create another file to turn on error reporting first, then include the other file with the problem:<?phperror_reporting(E_ALL);ini_set('display_errors', 1);include 'file.php';?>
I tried adding that to the top of my code and the screen went blank again so took it out.
You don't need a file with a specific name, you don't even need an external Javascript file at all, you just need to define the delete_record function. It doesn't matter where you define it as long as it's defined. You've already got the showHint function that sends an AJAX request. You need the delete_record function to send the ID to delete to your PHP script which does the actual delete. When the response comes back you can refresh the page, or remove the row from the table, or whatever you want to do.
function delete_record(id){  xmlHttp=GetXmlHttpObject();  if (xmlHttp==null)  {	alert ("Your browser does not support AJAX!");	return;  }   var url="delete.php?id=" + id;  xmlHttp.onreadystatechange=function ()  {	// check for response, delete row from html table, etc  };  xmlHttp.open("GET",url,true);  xmlHttp.send(null);}

I'm getting the same error using this code. The screen goes blank. If I take out the following line from the code;
echo "<td>"<a href="java script:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a>"</td>";

and revert back to the code before I added anything to do with the delete feature and it loads up like normal so it looks like the problems here. The code I'm using is below;MG.php

<html><head><title>Malik's Gym</title><script src="selectuser.js"></script><link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" /><script type="text/javascript" src="tablecloth/tablecloth.js"></script></head><body background="backgrounda.PNG">function delete_record(id){  xmlHttp=GetXmlHttpObject();  if (xmlHttp==null)  {	alert ("Your browser does not support AJAX!");	return;  }  var url="delete.php?id=" + id;  xmlHttp.onreadystatechange=function ()  {	// check for response, delete row from html table, etc  };  xmlHttp.open("GET",url,true);  xmlHttp.send(null);}<h2 align="center"><b><u><font size="8" face="COMIC SANS MS" font color="#38ACEC">Malik's Gym</font></b></u></h2><form><font color="#38ACEC"><b>Search:</b></font><select name="users" onchange="showUser(this.value)"><option value="1">Tahir Malik</option><option value="2">Khalid Mahmood</option><option value="3">Amanda Murphy</option><option value="4">Pep Messoud</option><option value="5">Nabeela Din</option></select></form><p><center><div id="txtHint"><?php$con = mysql_connect('localhost', 'tmalik2', '*****');if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("tmalik2", $con);$sql="SELECT * FROM Malik";$result = mysql_query($sql);echo "<table border='1'><tr><th>ID</th><th>Name</th><th>######</th><th>Age</th><th>Address</th><th>Post Code</th><th>Contact Number</th><th>Member Type</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>"<a href="java script:void(0);" onclick="delete_record(' . $row['ID'] . ');">Delete</a>"</td>";echo "<td>" . $row['Name'] . "</td>";echo "<td>" . $row['######'] . "</td>";echo "<td>" . $row['Age'] . "</td>";echo "<td>" . $row['Address'] . "</td>";echo "<td>" . $row['Post_Code'] . "</td>";echo "<td>" . $row['Contact_Number'] . "</td>";echo "<td>" . $row['Member_Type'] . "</td>";echo "</tr>";}echo "</table>";?></div></center></p><center><FORM METHOD="LINK" ACTION="http://samp.inf.brad.ac.uk:59237/add.php"><INPUT TYPE="submit" VALUE="Add Member"></FORM></center></body></html>

Thanks for your help!

Edited by diego
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...