Forum Moderators: coopster
This is my first php attempt. I am trying to adapt a script. The page I am stuck on, is the listing of records page. Instead of the first 100 records from EVERYONE to be posted, I only want the records of this user to be posted. I want the user to enter a ClassUsername and a ClassPassword, and only the database records that contain these two identifiers should be visible. Then the user could edit or delete any of his OWN records, but not have access to anyone else's records.
The other problem is, how to add checkboxes so multiple records could be deleted with one click, rather than deleting one record at a time.
Thanks so much! I've been reading tutorials for days now, and even the basic tutorials are above me!
<html>
<head>
<title>RRR</title>
</head>
<body bgcolor="white">
<h2>Record List</h2>
<a href='add.php'>Add A Record</a><br><br><br>
<?php
include("connect.php");
$sql = "SELECT * FROM abcd WHERE ID='$id' LIMIT 100";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
while($result = mysql_fetch_array($query)) {
$FirstName = stripslashes($result["FirstName"]);
$LastName = stripslashes($result["LastName"]);
$Email = stripslashes($result["Email"]);
$ClassUsername = stripslashes($result["ClassUsername"]);
$ClassPassword = stripslashes($result["ClassPassword"]);
$ID = $result["ID"];
echo "$FirstName $LastName $Email $ClassUsername $ClassPassword [<a href='edit.php?id=$ID'>Edit</a> ¦ <a href='delete.php?id=$ID'>Delete</a>]<br>";
}
?>
</body></html>
The way I did it was with Dreamweaver's PHP functionality.
I don't know if you have access to it or not, but I would give it a try if you get a chance.
Another good source is the PHP MySQL book by Luke Welling.
Good Luck
<?php
$colname_rsRecords = "1";
if (isset($_GET['ClassUsername'])) {
$colname_rsRecords = (get_magic_quotes_gpc())? $_GET['ClassUsername'] : addslashes($_GET['ClassUsername']);
}
mysql_select_db($database_connConnect, $connConnect);
$query_rsClient = sprintf("SELECT * FROM tablename WHERE ClassUsername = '%s'", $colname_rsRecords);
$rsClient = mysql_query($query_rsClient, $connConnect) or die(mysql_error());
$row_rsConnect = mysql_fetch_assoc($rsConnect);
$totalRows_rsConnect = mysql_num_rows($rsConnect);
?>
Currently, you are getting a list of all rows in the database because you are not matching rows to the variable you want.
The above will return all rows that match ClassUsername which is taken from the url with _GET. However, I would recommend you use sessions instead.
For the above to work you need to pass ClassUsername from the login page as a get variable.
<input type="checkbox" name="delete[]" value="$ID"> Delete
in your HTML.
When you POST the form to a script PHP will see an array called $delete so you can do
foreach ($delete as $ID) {
// Delete the appropriate record here
}
Obviously you need to validate their credentials at this stage too.
I have a script example that sounds like yours, but I am getting the error message:
Fatal error: Cannot use [] for reading in /home/mysite/public_html/folder/step2.php on line 5
Here's what I'm using:
<form action="http://mysite/folder/step2.php" method="POST">
<?
while ($data = mysql_fetch_array($sql)) {
?>
<input type="checkbox" name='toDelete[]' value="<? echo $data["ID"];?>"> ID: <? echo $data["ID"];?><br>
<?
}
?>
<input type="submit" name="Submit" value="DELETE ALL CHECKED BOXES">
</form>
And in step2.php :
<?
include("connect.php");
if ($submit) {
$theData = explode(",", $toDelete[]);
foreach ($theData as $deleteNumber) {
mysql_query("DELETE FROM homework WHERE ID='$deleteNumber'");
}
} else {
echo("You did not submit the form properly.");
exit;
}
?>
Does anyone know what the error message means, and how to correct it? Thanks!
I am at a loss as to what causes the error message:
Cannot use [] for reading ... If someone could please help me understand this, I would appreciate it!
I tried Netizen's suggestion, but being new to all this, I obviously did not include the correct phrases in addition to what Netizen graciously posted. I either got a parse error message, or a blank page upon posting...nothing was ever deleted.
Please help!
while ($data = mysql_fetch_array($sql)) {
printf('
<input type="checkbox" name="toDelete[]" value="%s"> ID: %s<br>',
$data['ID'],
$data['ID']
);
}
The printf command can make the code look a lot simpler. Essentially the first %s in it will get replaced by $data['ID'], and also the second. A more general example would be
printf('This is an %s and this is a %s','apple','pear');
would print:
This is an apple and this is a pear
Hope that helps.
Just returned to this site to post what I think may be one possible solution, and saw your post.
What I did was change step2.php to:
<?
include("connect.php");
if((bool)$_POST)
{
foreach($_POST['toDelete'] as $ID)
{
mysql_query('DELETE FROM table WHERE ID='.$ID);
}
}
?>
Now, I don't know what this means, or if it is the best way to delete checkboxes, but it seems to work. I've been searching Google for days, and found this suggestion. I've never even heard of bool! This code certainly looks like your "foreach" idea.
Netizen, now that I can breathe easier, I'm going to try your new suggestion too. I'm curious to see if that was my problem. I want to thank you so much for all of your help!