Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting into a database 1

Status
Not open for further replies.

thumbelina

Programmer
Jun 4, 2001
58
CA
ok i have a form with one text box, and i'm inserting it into the mysql database with php, but for every record i insert it also inserts a blank one. anythoughts??
 
Could you post your code please and how your mySQL table is set up. No one can tell from just a question.

Thanks,
-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
right sorry.
<form name=&quot;newproject&quot;method=post>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;2&quot; width=&quot;280&quot;>
<tr>
<td><label>New Project:</label></td>
<td><input type=&quot;text&quot; name=&quot;textbox&quot; size=&quot;24&quot;></td>
</tr>
<tr>
<td colspan=&quot;2&quot;><input type=&quot;submit&quot; value=&quot;Submit Project Name&quot;></td>
</FORM></tr>
<?php $query =&quot;INSERT INTO ProjectList(projectname) VALUES ('$textbox')&quot;;
$result =mysql_query($query,$connect);?>

the table is just 2 coloums
entryID which is primary key and auto increment
projectname which is a varchar or text field.
that's all it is. i also tried putting the insert code on a seperate page but that didn't work either.

 
I would set up my PHP like so:

You have:
[tt]
<?php $query =&quot;INSERT INTO ProjectList(projectname) VALUES ('$textbox')&quot;;
$result =mysql_query($query,$connect);?>
[/tt]

I would do;
[tt]
<?php

$database = &quot;yourdb&quot;;
$sql = &quot;INSERT INTO ProjectList (entryID,projectname) VALUES ('','$textbox')&quot;;
$do = mysql($database,$sql) or die(&quot;failed to issue query&quot;);
print &quot;query successful&quot;;
?>

[/tt]

Try that and tell me what you think.

Hope this helps,
-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hmm. As a last resort:

You should back up all of your data in the database, recreate the database along with all the tables and stuff, delete the database, and then recreate it and the tables and see what happens.

Do that and tell me what happens.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
I couldn't delete the database because I don't think I'm authorized to create another one, but i did delete the table and recreate it, and i'm still getting blank records.
 
I would like to ammend what it is now doing. it now only enters a blank record for each session on that page. for example i load the page enter a record and i get one record and one blank one. Then i enter 2 more records just fine. I leave the page and reload it and i get a blank record with my first record then the rest are fine again.
 
ok this is getting silly, but I figured out what it's doing now. when it loads the page it's running the query. it's not waiting for me to hit the submit button. any thoughts?
 
Well, if the query is not wrapped in some type of container (a function, an if statement, a loop), then it will always execute.

If you just have

<?php
# code for inserting here
?>

Then everytime you go to that page, it will insert a record. Obviously, if there are no variables to insert into the database, then you will get a black record.

However, if you wrap your insertion code into a function and then call the function like so, that may work:

<?php
function insert($data) {
$sql = &quot;INSERT INTO ....&quot;;
// continue with code
}

Then call function like so:

insert($textbox);

It will do the function and everything should be fine and dandy.

You should also check to see if the variable you are inserting has a value, and if not, don't insert it:

if(!isset($textbox)) {
// dont query the value into the db
} else {
// do query the value into the db
}

I think that should do it. If it keeps doing that, I don't know what you say. Perhaps post the site that it is happening at or email me all of the code at krs-one@cnunited.com and I will take a look at it.

Hope this helps.

-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top