Re: Please, answer my question

From: Date: Mon, 26 Apr 2004 08:12:22 +0000
Subject: Re: Please, answer my question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-184609@lists.php.net to get a copy of this message
Try working explicitely with the MySQL link identifier you get from mysql_connect(). See: http://de.php.net/manual/en/function.mysql-connect.php and http://de3.php.net/manual/en/function.mysql-query.php >>> "S. M." <marewsh@yahoo.com> wrote in message news:20040426080245.16273.qmail@web12708.mail.yahoo.com... I'll be appritiate if you could help me in solving my problem :) I have the following script using php to add announcements to the database . The problem is the script does not match with the database , i mean it does not insert or update or even delete what i have (the aim of the script) . I do not know if there is a problem with the representation of the variables .Because iam sure that my database works pefectly . If appritiate if you look to my script and replying me if you have any ideas to be corrected or added for my graduation project with best regars <?php #include 'include.inc'; # mySQL host address (usually 'localhost') $db_host = "localhost"; # mySQL database name $db_name = "e_submission"; # mySQL database username $db_username = "root"; # mySQL database password $db_password = "esub"; # option to enable link to the to-do editor # to enable/disable, enter '1' to ENABLE or '0' to DISABLE (without quotes) $opt_link = 1; function db_connect() { // Open a connection to the DBMS global $db_host, $db_name, $db_username, $db_password; mysql_connect($db_host, $db_username, $db_password) or die ("<font color=red>Cannot connect to mySQL server</font>"); mysql_select_db($db_name) or die ("<font color=red>Cannot connect to mySQL database</font>"); } function todo() { global $db_host, $db_name, $db_username, $db_password, $opt_link; echo"<html>\n<head>\n<title>TO-DO List</title>\n</head>\n\n<body link=#C0C0C0 vlink=#C0C0C0 alink=#C0C0C0>\n"; echo"<font face=tahoma,arial><center><h2>TO-DO List</h2>\n\n"; db_connect(); $sql = "SELECT * FROM announcement ORDER BY id DESC LIMIT 0, 30"; $result = mysql_query($sql) or die ("Unknown mySQL error"); mysql_num_rows($result) or die ("<i>Nothing is currently on the to-do list</i> - <a href=$_SERVER[PHP_SELF]?do=edit>Edit the list</font></a>"); echo"<table border=3 cellspacing=3 cellpadding=2 bordercolor=#000000 bgcolor=#E0E0E0 width=80% style=\"font-size: 14px; border-collapse:collapse;\">\n<tr><td width=75><b>ID</b></td><td width=430><b>Title</b></td><td width=330><b>Content</b></td><td width=154><b>Date</b></td></tr>\n"; while ($row = mysql_fetch_array($result)) { echo"<tr bordercolor=#B9B3A2><td width=75>$row[id]</td><td width=430>$row[title]</td><td width=330>$row[content]</td><td width=154>$row[date_time]</td></tr>\n"; } if ($opt_link == 1) echo"</table>\n\n<br><br><a href=$_SERVER[PHP_SELF]?do=edit><font color=black>TO-DO List Editor</font></a><br>"; } else { echo"</font></table>\n\n<br><br>"; } echo"<font size=1 color=#C0C0C0>Powered by <a href=http://mtnpeak.net>TO-DO List v0.2</a></font></font>\n\n</body>\n</html>"; } function todoedit() { global $db_host, $db_name, $db_username, $db_password; echo"<html><head><title>TO-DO List / editor</title></head> <body link=#C0C0C0 vlink=#C0C0C0 alink=#C0C0C0><center><b><font face=tahoma, arial> <center><b><h2>TO-DO List</b> / editor</h2></center> <table border=3 bordercolor=\"#000000\" style=\"border-collapse: collapse\" cellpadding=\"4\" cellspacing=\"1\" height=\"450\"><tr> <td width=50% height=\"441\"> <font> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Add</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td>X</td><td><input type=text name=\"item1\" size=45></td><td><input type=text name=\"date1\" size=10></td></tr> <tr><tr><td></td><td><font size=\"2\">Content:</font><br><input type=text name=\"notes1\" size=45></td></tr> <tr><td></td><td align=center><input type=submit value=Add name=add></td><td></td></tr></table><br><br><br> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Update</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td><input type=text name=\"id2\" size=1></td><td><input type=text name=\"item2\" size=45></td><td><input type=text name=\"date2\" size=10></td></tr> <tr><tr><td></td><td><font size=\"2\">Content:</font><br><input type=text name=\"notes2\" size=45></td></tr> <tr><td></td><td align=center><input type=submit value=Update name=update></td><td></td></tr></table><br><br><br> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Delete</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td><input type=text size=1 name=id></td><td><font size=\"2\">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</font></td><td> <font size=\"2\">XXXXXXX</font></td></tr> <tr><td></td><td align=center> <input type=submit value=Delete name=delete style=\"color: #FF0000\"></td><td></td></tr></table> </td><td width=50% valign=top height=\"441\"> <B>TO-DO List Preview Pane</b>&nbsp;&nbsp;&nbsp;<font size=1><a href=$_SERVER[PHP_SELF]?do=edit>Refresh</a></font>"; db_connect(); $sql = "SELECT * FROM announcement ORDER BY id DESC"; $result = mysql_query($sql) or die ("Unknown mySQL error"); mysql_num_rows($result) or die ("<br><i>Nothing is currently on the to-do list</i>"); echo"<table border=1 bordercolor=\"#C0C0C0\" style=\"font-size: 12px; border-collapse: collapse\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=30><b>ID</b></td><td width=300><b>Item</b></td><td width=170><b>Content</b></td><td width=80><b>Date</b></td></tr></font>"; while ($row = mysql_fetch_array($result)) { echo"<tr><td><font size=1 face=courier>$row[id]</font></td><td><font size=1 face=courier>$row[title]</font></td><td><font size=1 face=courier>$row[content]</font></td><td><font size=1 face=courier>$row[date_time]</font></td></tr>"; } echo"</td></tr></table><br><br><center><a href=$_SERVER[PHP_SELF]><font color=black>TO-DO List</font></a><br><font size=1 color=#C0C0C0></font></center></table></body></html>"; } if ($_GET['do']=="edit") { if(isset($add)) { db_connect(); $sql = "INSERT INTO announcement (title, date_time, content) VALUES ('$item1', '$date1', '$notes1')"; mysql_query($sql); todoedit(); } elseif(isset($update)) { db_connect(); $sql = "UPDATE announcement SET title='$item2', date_time='$date2', content='$notes2' WHERE id='$id2'"; mysql_query($sql); todoedit(); } elseif(isset($delete)) { db_connect(); $sql = "DELETE FROM announcement WHERE id='$id'"; mysql_query($sql); todoedit(); } elseif(!isset($add) || !isset($delete) || !isset($update)) { todoedit(); } } else { todo(); } ?> Do you Yahoo!? Yahoo! Photos: High-quality 4x6 digital prints for 25¢ <?php #include 'include.inc'; # mySQL host address (usually 'localhost') $db_host = "localhost"; # mySQL database name $db_name = "e_submission"; # mySQL database username $db_username = "root"; # mySQL database password $db_password = "esub"; # option to enable link to the to-do editor # to enable/disable, enter '1' to ENABLE or '0' to DISABLE (without quotes) $opt_link = 1; function db_connect() { // Open a connection to the DBMS global $db_host, $db_name, $db_username, $db_password; mysql_connect($db_host, $db_username, $db_password) or die ("<font color=red>Cannot connect to mySQL server</font>"); mysql_select_db($db_name) or die ("<font color=red>Cannot connect to mySQL database</font>"); } function todo() { global $db_host, $db_name, $db_username, $db_password, $opt_link; echo"<html>\n<head>\n<title>TO-DO List</title>\n</head>\n\n<body link=#C0C0C0 vlink=#C0C0C0 alink=#C0C0C0>\n"; echo"<font face=tahoma,arial><center><h2>TO-DO List</h2>\n\n"; db_connect(); $s ql = "SELECT * FROM announcement ORDER BY id DESC LIMIT 0, 30"; $result = mysql_query($sql) or die ("Unknown mySQL error"); mysql_num_rows($result) or die ("<i>Nothing is currently on the to-do list</i> - <a href=$_SERVER[PHP_SELF]?do=edit>Edit the list</font></a>"); echo"<table border=3 cellspacing=3 cellpadding=2 bordercolor=#000000 bgcolor=#E0E0E0 width=80% style=\"font-size: 14px; border-collapse:collapse;\">\n<tr><td width=75><b>ID</b></td><td width=430><b>Title</b></td><td width=330><b>Content</b></td><td width=154><b>Date</b></td></tr>\n"; while ($row = mysql_fetch_array($result)) { echo"<tr bordercolor=#B9B3A2><td width=75>$row[id]</td><td width=430>$row[title]</td><td width=330>$row[content]</td><td width=154>$row[date_time]</td></tr>\n"; } if ($opt_link == 1) { echo"</table>\n\n<br><br><a href=$_SERVER[PHP_SELF]?do=edit><font color=black>TO-DO List Editor</font></a><br>"; } else { echo"</font></table>\n\n<br><br>"; } echo"<font size=1 color=#C0C0C0>Powered by <a href=http://mtnpeak.net>TO-DO List v0.2</a></font></font>\n\n</body>\n</html>"; } function todoedit() { global $db_host, $db_name, $db_username, $db_password; echo"<html><head><title>TO-DO List / editor</title></head> <body link=#C0C0C0 vlink=#C0C0C0 alink=#C0C0C0><center><b><font face=tahoma, arial> <center><b><h2>TO-DO List</b> / editor</h2></center> <table border=3 bordercolor=\"#000000\" style=\"border-collapse: collapse\" cellpadding=\"4\" cellspacing=\"1\" height=\"450\"><tr> <td width=50% height=\"441\"> <font> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Add</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td>X</td><td><input type=text name=\"item1\" size=45></td><td><input type=text name=\"date1\" size=10></td></tr> <tr><tr><td></td><td><font size=\"2\">Content:</font><br><input type=text name=\"notes1\" size=45></td></tr> <tr><td></td><td align =center><input type=submit value=Add name=add></td><td></td></tr></table><br><br><br> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Update</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td><input type=text name=\"id2\" size=1></td><td><input type=text name=\"item2\" size=45></td><td><input type=text name=\"date2\" size=10></td></tr> <tr><tr><td></td><td><font size=\"2\">Content:</font><br><input type=text name=\"notes2\" size=45></td></tr> <tr><td></td><td align=center><input type=submit value=Update name=update></td><td></td></tr></table><br><br><br> <form action=\"$_SERVER[PHP_SELF]?do=edit\" method=\"post\"> <b>Delete</b> <table border=0><tr><td width=28><font size=\"2\">ID</font></td><td width=300> <font size=\"2\">Title</font></td><td width=80><font size=\"2\">Date</font></td></tr> <tr><td><input type=text size=1 name=id></td><td><font size=\"2\">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</font></td><td> <font size=\"2\">XXXXXXX</font></td></tr> <tr><td></td><td align=center> <input type=submit value=Delete name=delete style=\"color: #FF0000\"></td><td></td></tr></table> </td><td width=50% valign=top height=\"441\"> <B>TO-DO List Preview Pane</b>&nbsp;&nbsp;&nbsp;<font size=1><a href=$_SERVER[PHP_SELF]?do=edit>Refresh</a></font>"; db_connect(); $sql = "SELECT * FROM announcement ORDER BY id DESC"; $result = mysql_query($sql) or die ("Unknown mySQL error"); mysql_num_rows($result) or die ("<br><i>Nothing is currently on the to-do list</i>"); echo"<table border=1 bordercolor=\"#C0C0C0\" style=\"font-size: 12px; border-collapse: collapse\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=30><b>ID</b></td><td width=300><b>Item</b></td><td width=170><b>Content</b></td><td width=80><b>Date</b></td></tr></font>"; while ($row = mysql_fetch_array($result)) { echo"<tr><td><font size=1 face=courier>$row[id]</font></td><td><font size=1 face=courier>$row[title]</font></td><td><font size=1 face=courier>$row[content]</font></td><td><font size=1 face=courier>$row[date_time]</font></td></tr>"; } echo"</td></tr></table><br><br><center><a href=$_SERVER[PHP_SELF]><font color=black>TO-DO List</font></a><br><font size=1 color=#C0C0C0></font></center></table></body></html>"; } if ($_GET['do']=="edit") { if(isset($add)) { db_connect(); $sql = "INSERT INTO announcement (title, date_time, content) VALUES ('$item1', '$date1', '$notes1')"; mysql_query($sql); todoedit(); } elseif(isset($update)) { db_connect(); $sql = "UPDATE announcement SET title='$item2', date_time='$date2', content='$notes2' WHERE id='$id2'"; mysql_query($sql); todoedit(); } elseif(isset($delete)) { db_connect(); $sql = "DELETE FROM announcement WHERE id='$id'"; mysql_query($sql); todoedit(); } elseif(!isset($add) || !isset($delete) || !isset($update)) { todoedit(); } } else { todo(); } ?>

« previous php.general (#184609) next »