adding data to a mysql table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscar
Forum Commoner
Posts: 27
Joined: Wed Sep 24, 2003 7:27 pm
Location: sydney, Australia

adding data to a mysql table?

Post by oscar »

i've recently embarked on a quest to both learn php& mysql AND do somethng usefull with it (for once... i have a habit of learning programming languages & not finishing what i set out to do with it :P )

anyway... i've started coding a search engine

http://oscarent-host-dot.com/search (search "all" to show all 5 entries in the database)

and everything is working fine except... when i made a script for people to submit urls to the database so i don't have to do it manually, the script doesn't add any data to the table... i checked on mysql.com but according to the examples i'm doing it right

Code: Select all

<form name="form1" method="post" action="
<?php

// i'm trying to connect to database: oscarent_titansearch, table: search, with user: oscarent_admin with no  password

$dbh=mysql_connect ("localhost", "oscarent_admin", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("oscarent_titansearch");
mysql_query("INSERT INTO search VALUES(12,$sitename,$siteurl, $sitedescrip,$keywords) or die('error: '  . mysql_error()");
?>">

   <p class="sub"><span class="sub4"><strong>Data For Site Submission to Titan Search: </strong></span><br>
            <br>
            Site Name<br>
            <input name="sitename" type="text" size="50" maxlength="50">
            <br>
            <br>
            Site Address(URL):<br>
<input name="siteurl" type="text" size="50" maxlength="100">
   </p>
          <p class="sub">Description:<br>
            <input name="sitedescrip" type="text" id="textfield" size="100" maxlength="200">
<br>
            <br>
            Keywords (insert as CSV's eg.[wod, word2, grand]):<br>
            <textarea name="keywords" cols="100" rows="6"></textarea>            
            <input name="imageField" type="image" src="http://www.nike-philips.com/en/images/submit_button.gif" border="0">
          </p>
</form>

btw all database names, table names, passwords, etc are correct...

AND

the 12 at the start of the insert is the primary key for the db... i couldn't figure out how to read the highest existing value & add 1

can anyone please help me :D
:P
thanks
Last edited by oscar on Mon Apr 04, 2005 7:45 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need single quotes around each value :wink:

EDIT | In fact

Code: Select all

mysql_query("INSERT INTO search VALUES(12,$sitename,$siteurl,$sitedescrip,$keywords) or die('error: '  . mysql_error()");
Is all wrong (it should be this....):

Code: Select all

mysql_query("INSERT INTO search VALUES('12','$sitename','$siteurl','$sitedescrip','$keywords')") or die('error: '  . mysql_error());
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Whoooaaa boy what's this?

Code: Select all

action="
<?php

// i'm trying to connect to database: oscarent_titansearch, table: search, with user: oscarent_admin with no  password

$dbh=mysql_connect ("localhost", "oscarent_admin", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("oscarent_titansearch");
mysql_query("INSERT INTO search VALUES(12,$sitename,$siteurl, $sitedescrip,$keywords) or die('error: '  . mysql_error()");
?>"
That won't work..

action="yourfile.php" with yourfile.php containing the relevant code will work however....

You can't just whack the php process script in the action attribute... :D
Last edited by Chris Corbyn on Mon Apr 04, 2005 7:55 am, edited 2 times in total.
oscar
Forum Commoner
Posts: 27
Joined: Wed Sep 24, 2003 7:27 pm
Location: sydney, Australia

Post by oscar »

thanks man :oops: kinda a stupid mistake now that i can see it


... i feel violated by the simpleness of this mistake :P

:D
oscar
Forum Commoner
Posts: 27
Joined: Wed Sep 24, 2003 7:27 pm
Location: sydney, Australia

Post by oscar »

It worked in my contact script though ?... i jsut whacked the code into teh action tag...

Code: Select all

<form name="form1" method="post" action="
<? 
	  $header = "From: $textfield3";
	  mail($select,$textfield,$textarea,$header);
	  ?>
	  ">
          <p class="sub"><span class="sub4"><strong>For questions and inquiries:</strong></span><br>
            <br>
            Your email:<br>
            <input name="textfield3" type="text" size="15" maxlength="20">
            <br>
            <br>
            Select who you wish to contact:<br>
            <select name="select">
              <option value="grantmcnally@gmail.com" selected>Webmaster (Grant McNally)</option>
			<option value="grantmcnally@gmail.com">Shop</option>
			<option value="grantmcnally@gmail.com">Bug Report</option>
			<option value="grantmcnally@gmail.com">Join Network (affiliation)</option>
            </select>
          </p>
          <p class="sub">Subject:<br>
            <input name="textfield" type="text" id="textfield" size="15" maxlength="20">
<br>
            <br>
            Enter your questions and comments below:<br>
            <textarea name="textarea" cols="50" rows="6"></textarea>
            <input name="imageField" type="image" src="http://www.nike-philips.com/en/images/submit_button.gif border="0">
          </p>
          <p align="right" class="bod"> <br>
          </p>
        </form>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah.... that's not the action attribute that's doing ti.

See... any PHP code <?php ?> in the page gets parsed when the page is requested.

Putting no filename in the action attribute means it send the data to itself (generally). Therefore after sending to itself it executes the code.

Note... if it wasn't submitted yet it would execute the code with errors.

Use action="<?=$_SERVER['PHP_SELF']; ?>" and then put:

Code: Select all

if (!empty($_POST)) {
    //Do the work
}
somewhere else in the page if want the same page to proccess the info.... :wink:
oscar
Forum Commoner
Posts: 27
Joined: Wed Sep 24, 2003 7:27 pm
Location: sydney, Australia

Post by oscar »

:oops: that nakes sense :P


thanks
Post Reply