View Full Version : PHP Insert Database
Needing to insert into a database, from a webpage using PHP :|
Anyone got any nice code :? :P
clicky (http://php.resourceindex.com/)
This is **** :x
mysql_connect("mysql6.streamline.net",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = INSERT INTO auction_items VALUES ($titleid,$memberid,$auctionenddate,$condition,$st artingprice,$reserveprice,$comments);
^^ MY PHP CODE To Insert From Form
All happens is that you click submit, it goes to a white page. No errors by the look, go to look in the database and its not updated :(
Mercuryblade
01-05-2006, 15:30
try this, it's what i use on one of my pages.
<?
$db_username = "user";
$db_password = "password";
$db_hostname = "mysql6.streamline.net";
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or
die("Unable to connect to MySQL");
$db_select = mysql_select_db("database_name", $db_connect)or die("Unable to select table");
mysql_query("insert into auction_items values('$titleid','$memberid','$auctionenddate','$ condition','$st artingprice','$reserveprice','$comments')");
mysql_close($db_connect);
?>
obviously enter your own username, password and database name.
I've changed the insert statement for you already and the hostname for the database.
Again I get the white page with no error messages :( But no record inserted :cry:
Seems to be some improvement,
Its inserting a record full of '0' if the field is set to default to Not Null.
So I presuming its not reading the form fields?
Mercuryblade
01-05-2006, 15:45
make sure there are no spaces where they shouldn't be,
i've just noticed '$st artingprice' which should be '$startingprice'
also do you want to send me the page, then I can change the database setting to point to mine and have a fiddle with it for you.
http://www.isaunderson.co.uk/HTML/sell.html
Only basic uni page but this PHP Is pissing me off :lol:
Do you want the PHP File as wel?
In the form page, should the names of the form fields be "$titleid" or $titleid or "titleid" :?
So bloody confusing!!
Mercuryblade
01-05-2006, 15:52
send me the php page please, otherwise I can't look at the code
EDIT: the "$titleid" format
The only code I have in the PHP file is the bit you have given me :?
Then the form action is set to the PHP file?
Mercuryblade
01-05-2006, 22:23
had a bit of a play about with this but can't seem to get it to work,
It works when just inserting data into the database,
but when I tried to use the form post and declared each post field as a variable it just didn't want to work.
I even downloaded a script pre-made to do this and that did the same so fudge knows whats wrong.
Will have another look if i get chance, probably something simple thats been missed.
Tutors helped me get that bit to work, but foreign keys in the database wont allow me to insert the record due to the Title ID not existing.
So I need to insert into Titles table before Auction Items:
$sql2 = "insert into titles
values($titleid,'$booktitle','$datepublished','$au thor','$edition')";
echo $sql2 . "<hr/>";
mysql_query($sql2, $dbconnect);
echo mysql_error() . "<hr/>";
$sql = "insert into auction_items
values($titleid,'$memberid','','$auctionenddate',' $condition',$startingprice,$reserveprice,'$comment s')";
echo $sql . "<hr/>";
mysql_query($sql,$db_connect);
echo mysql_error() . "<hr/>";
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in E:\students_20052006\SaundersonI\HTML\sell.php
Another error :x
Mercuryblade
02-05-2006, 12:07
could it be that in the second query your trying to insert data into 8 fields on the table 'auction_items' which only has 7 fields.
Wahey got it working :D
It was the $db_connect, top one I used $dbconnect without the underscore.
Thanks very much MercuryBlade :thumbs: Owe you a pint at the Next show.
Mercuryblade
02-05-2006, 22:07
lol didn't really help you tho :|
i just attempted to lol :lol:
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.