home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Script Help with MySQL and PHP

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1663
Discussions: 200,957, Posts: 2,379,495, Members: 246,327
Old March 12th, 2002, 03:26 PM   Digg it!   #1 (permalink)
Ultimate Member
 
Join Date: May 2000
Location: Miami, FL.
Posts: 2,841
Send a message via ICQ to voogru Send a message via AIM to voogru Send a message via MSN to voogru
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?

__________________
My Website

Last edited by voogru : March 12th, 2002 at 03:33 PM.
voogru is offline   Reply With Quote
Old 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.
dragonb is offline   Reply With Quote
Old March 12th, 2002, 10:48 PM     #3 (permalink)
Banned
 
qball's Avatar
 
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)
qball is offline   Reply With Quote
Old March 13th, 2002, 12:44 AM     #4 (permalink)
Ultimate Member
 
Join Date: May 2000
Location: Miami, FL.
Posts: 2,841
Send a message via ICQ to voogru Send a message via AIM to voogru Send a message via MSN to voogru
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.
voogru is offline   Reply With Quote
Old 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
dragonb is offline   Reply With Quote
Old March 13th, 2002, 08:21 AM     #6 (permalink)
Ultimate Member
 
Join Date: May 2000
Location: Miami, FL.
Posts: 2,841
Send a message via ICQ to voogru Send a message via AIM to voogru Send a message via MSN to voogru
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?
voogru is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2947)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
Print spooler problem (15)
windows vista security holes (11)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
Open With ..... Win7 (1)
Kingston Bluetooth Dongle Driver (0)
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)


All times are GMT -4. The time now is 07:34 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28