Forum Moderators: coopster

Message Too Old, No Replies

First php attempt

         

oceanwave

7:59 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



Hi,

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&nbsp;&nbsp;A&nbsp;&nbsp;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>

yowza

6:55 am on Feb 14, 2004 (gmt 0)

10+ Year Member



I'm a beginner too. However, I just created a full ecommerce site in php with mysql.

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

oceanwave

1:41 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Thanks for responding! Unfortunately, I do not have access to Dreamweaver. I will try to track down a copy of that book. Thanks for the suggestion.

Does anyone have a coding idea to start me in the right direction?

Thank you!

yowza

7:23 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



It seems that you need to pass ClassUsername from the login page to the results page as a cookie, GET variable, or session variable. When you have this variable you can match all database records to that variable. Something like this:

<?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.

oceanwave

5:19 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Thank you so much Yowza!

I'm going to work on it today to see if I can figure out your coding.

You said sessions might be a better choice. Why? Is there example code anywhere?

To think, two weeks ago I didn't even know how to create a table!

Netizen

3:20 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



You would have something like

<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.

oceanwave

1:55 pm on Feb 19, 2004 (gmt 0)

10+ Year Member



Thanks Netizen! I've been working on this checkbox thing. My problem is, when I read an answer, I'm not sure where to put it in the script, or how to adapt it properly. As you can tell, I've only been studying php for a couple of weeks.

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!

oceanwave

5:40 am on Feb 20, 2004 (gmt 0)

10+ Year Member



The name="toDelete[]" should have quotes around it (still gets the error message). I forgot to change that before I pasted it into my post. Sorry!

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!

Netizen

2:03 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Try escaping the ['s and ]'s as you are using them in a " quoted string. Or use magical printf command:

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.

oceanwave

7:28 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Hi Netizen,

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!