Script Help with MySQL and PHP  | |
March 12th, 2002, 03:26 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: May 2000 Location: Miami, FL.
Posts: 2,841
| Script Help with MySQL and PHP
Heres my script PHP Code: <? include("header.php"); ?>
<br>
<br>
<center>
<img src="http://www.tinkerbell.ws/images/links.gif">
</center>
<?php
$db = mysql_connect("localhost","voogru","password");
//The function mysql_select_db() sets the current
//databse to the one specified in this case "samplelinks"
mysql_select_db("voogru_tinkerbell" ,$db);
$sqlquery = mysql_query("SELECT * FROM links" ,$db);
echo " <center>\n";
echo "<table border ='0' bgcolor=\"#9999FF\" cellspacing=\"1\" width=\"60%\">\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><b><font color=\"#FFFF00\">Link</font></b></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\"><b>Description</b></font></td>\n";
while ($tablerows = mysql_fetch_row($sqlquery))
{
echo " </tr>\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><a href='$tablerows[0]'>$tablerows[0]</a></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\">$tablerows[1]</font></td>\n";
echo " </tr>\n";
}
//close the database
echo "</table>";
echo " </center> \n";
?>
<center>
<form action="links.php" method ="POST">
Link: <input type = "text" name = "link" class=voo_input maxlength="50"><br>
Description: <input type = "text" name = "description" class=voo_input maxlength="50"><br>
<input type = "submit" value = "add link" class="voo_input"><br>
</form>
</center>
<? include("footer.php"); ?>
<?
//make our connection details
$connection = mysql_connect("localhost" , "voogru" , "password")
or die ("Cannot make the connection");
//connect to sampledb with our connection details
if($url)
$sql_query = "INSERT into links VALUES ('url','$url')";
$result = mysql_query($sql_query);
if($description)
$sql_query = "INSERT into links VALUES ('description','$url')";
$result = mysql_query($sql_query);
?> Edit: fixed it, the fixed code is put above, im trying to it to put the data in the mysql tables correctly,
i have:
voogru_tinkerbell and in there i have a links table, with url, and description properties. but it seems it doesnt put it in right.
any ideas?
Last edited by voogru : March 12th, 2002 at 03:33 PM.
|
| |
March 12th, 2002, 05:18 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Utah
Posts: 551
|
Just on first glance, shouldn't the if($url) be if($link)? Also, you need brackets if you want both lines to run only on if, after the if statement.
Also, if there's both a url and a description, it would run both the if statements, I believe that would put both of them in the database under 2 separate instances? Is this what's happening?
If so, I would do it more like this...
if($description)
{
$sql_query = "INSERT into links (url, description) VALUES ('$link','$description')";
}
else
{
$sql_query = "INSERT into links VALUES ('url','$link')";
}
$result = mysql_query($sql_query);
edit: This would also assume that you've done error checking to insure there's a $url(or $link) otherwise you wouldn't want to run a database query in the first place. Also, I would stay away from using a variable called $url since that could be used in some programming languages. Something like my_url, db_url, f_url or some syntax that you could use all the time makes it a little more readable.
Also,
good luck,
dragonb
Last edited by dragonb : March 12th, 2002 at 05:24 PM.
|
| |
March 12th, 2002, 10:48 PM
|
#3 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
first off, what doesn't work? Quote: |
but it seems it doesnt put it in right
| second, your INSERT stmt is not a valid SQL stmt. From MySQL manual: Code: INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES (expression,...),(...),...
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name
SET col_name=expression, col_name=expression, ... Maybe:
INSERT into links ('url', 'description') VALUES ( $url, $description) |
| |
March 13th, 2002, 12:44 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: May 2000 Location: Miami, FL.
Posts: 2,841
|
i got it working now, had some troubles but fixed it wit ha meta refresh hehe,
anyways here goes:
this is my links.php that the normal users see: PHP Code: <? include("header.php"); ?>
<br>
<br>
<center>
<img src="http://www.tinkerbell.ws/images/links.gif">
</center>
<?php
$db = mysql_connect("localhost","voogru","Passwords");
//The function mysql_select_db() sets the current
//databse to the one specified in this case "samplelinks"
mysql_select_db("voogru_tinkerbell" ,$db);
$sqlquery = mysql_query("SELECT * FROM links" ,$db);
echo " <center>\n";
echo "<table border ='0' bgcolor=\"#9999FF\" cellspacing=\"1\" width=\"60%\">\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><b><font color=\"#FFFF00\">Link</font></b></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\"><b>Description</b></font></td>\n";
while ($tablerows = mysql_fetch_row($sqlquery))
{
echo " </tr>\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><a href='$tablerows[0]'>$tablerows[0]</a></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\">$tablerows[1]</font></td>\n";
echo " </tr>\n";
}
//close the database
echo "</table>";
echo " </center> \n";
include("footer.php"); ?> this is in the admin panel, not very advanced but it works pretty good: PHP Code: <?
include("header.php");
$db = mysql_connect("localhost","voogru","Password");
//The function mysql_select_db() sets the current
//databse to the one specified in this case "samplelinks"
mysql_select_db("voogru_tinkerbell" ,$db);
$sqlquery = mysql_query("SELECT * FROM links" ,$db);
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
echo "<h2> Current Links</h2>\n";
echo " <center>\n";
echo "<table border ='0' bgcolor=\"#FFFF00\" cellspacing=\"1\" width=\"100%\">\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><b><font color=\"#FFFF00\">Link</font></b></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\"><b>Description</b></font></td>\n";
while ($tablerows = mysql_fetch_row($sqlquery))
{
echo " </tr>\n";
echo " <tr> \n";
echo " <td bgcolor=\"#000000\"><a href='$tablerows[0]'>$tablerows[0]</a></td>\n";
echo " <td bgcolor=\"#000000\"><font color=\"#FFFF00\">$tablerows[1]</font></td>\n";
echo " </tr>\n";
}
//close the database
echo "</table>";
echo " </center> \n";
?>
<center>
<form action="links.php?action=add" method ="POST">
<table width="100%" border="0" cellpadding="4" cellspacing="1">
<tr>
<td width="354" height="28"></td>
<td width="101" valign="top" bgcolor="#000000">
<div align="left"><font color="#FFFF00">Link: </font></div>
</td>
<td valign="top" width="132" bgcolor="#000000"><font color="#FFFF00">
<input type ="text" name ="url" class=voo_input maxlength="50" size="40" value="http://">
</font></td>
<td width="482"></td>
</tr>
<tr>
<td height="28"></td>
<td valign="top" bgcolor="#000000">
<div align="left"><font color="#FFFF00">Description: </font> </div>
</td>
<td valign="top" bgcolor="#000000"><font color="#FFFF00">
<textarea name="description" class="voo_input" cols="30" rows="5"></textarea>
</font></td>
<td></td>
</tr>
<tr>
<td height="33"></td>
<td colspan="2" valign="top" bgcolor="#000000">
<div align="center">
<input type = "submit" value = "Add Link" class="voo_input" name="submit">
</div>
</td>
<td></td>
</tr>
</table>
</form>
<form action="links.php?action=remove" method ="POST">
<table width="100%" border="0" cellpadding="4" cellspacing="1"
>
<tr>
<td width="316" height="46"></td>
<td width="100" valign="top" bgcolor="#000000">
<div align="center"><font color="#FFFF00">Delete Links </font> </div>
<div align="left"></div>
</td>
<td valign="top" width="230" bgcolor="#000000"><font color="#FFFF00">
<input type = "text" name="delete" maxlength="50" size="40" value="http://">
<br>
<center><input type="submit" value="Delete link"></center>
</font></td>
<td width="436"></td>
</tr>
</table>
</form>
</center>
<?
if ($action=="add"){
$connection = mysql_connect("localhost" , "voogru" , "Password")
or die ("Cannot make the connection");
$sql_query = "INSERT into links VALUES('$url','$description')";
$result = mysql_query($sql_query);
echo "<META HTTP-EQUIV=\"refresh\" content=\"0;URL=http://www.tinkerbell.ws/admin/links.php\"> \n";
}
if ($action=="remove"){
$connection = mysql_connect("localhost" , "voogru" , "Password")
or die ("Cannot make the connection");
$db = mysql_selectdb("voogru_tinkerbell" , $connection)
or die ("Cannot connect to the database");
//if the text field wasnt blank enter data
if($delete)
$sql_query = "DELETE FROM links WHERE url = ('$delete')";
$result = mysql_query($sql_query);
echo "<META HTTP-EQUIV=\"refresh\" content=\"0;URL=http://www.tinkerbell.ws/admin/links.php\"> \n";
}
?> EDIT: forgot to take out passwords lmao 
Last edited by voogru : March 13th, 2002 at 12:46 AM.
|
| |
March 13th, 2002, 01:17 AM
|
#5 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Utah
Posts: 551
|
looks good.
What's the meta tag refresh thing do?
thx
dragonb |
| |
March 13th, 2002, 08:21 AM
|
#6 (permalink)
| | Ultimate Member
Join Date: May 2000 Location: Miami, FL.
Posts: 2,841
|
i had a problem when the form was submitted it would not show up, but then if the user hit refresh it would show it. but then it would add another entry, im sure a meta refresh is the the best way but the only way i could figure out how to do to aviod dupelicate entries.
is there a way to make it flush its memory after being refreshed? |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |