TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Variables not writing to db (http://www.talkphp.com/absolute-beginners/2078-variables-not-writing-db.html)

d4v1d 01-24-2008 09:06 AM

Variables not writing to db
 
Hey guys... I've finally been able to get some time to play around with php a bit. I've hit a bit of a blank wall though, and was hoping someone could point out my obvious blunder. Taking into consideration the code below (I'm building it in stages, so it's not secure yet with regards to mysqli_real_escape_string), I am not able to write the variable value into the database. I've tried a few variations of the code, but still nothing. If I change the variable value to a normal string value, it inserts it without a prob. The code gives no errors either, so I'm a little confused :-/

PHP Code:

<?php
if ($_POST["submit"]) {
$hostname "localhost";
$username "david";
$password "test";
$dbname "test";

$insertSQL sprintf("INSERT INTO david_test (david_name) VALUES (%s)"$_POST['Fname']);
                       
$dbh mysqli_connect($hostname,$username,$password,$dbname) or die("Problem connecting: ".mysqli_error());
$result mysqli_query($dbh$insertSQL);

mysqli_close($dbh);}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" 
name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" 
name="Lname"><br />
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>


Alan @ CIT 01-24-2008 10:03 AM

Hi David,

Does it give any errors at all? If not, can you put an " or die(mysqli_error());" after your mysqli_query() - that may give us a clue to where it's going wrong :-)

Alan

d4v1d 01-24-2008 10:29 AM

It's now giving me an 'unknown column in fields list' error... Which means its associating my input with the field and not the value :-/ Any idea why? The sprintf looks right to me, so does the sql.

d4v1d 01-24-2008 10:55 AM

Ah, never mind... I forgot to add in the inverted commas in the sprintf... *!*

PHP Code:

$insertSQL sprintf("INSERT INTO david_test (david_name) VALUES ('%s')"$_POST['Fname']); 

Sorry about that. I'm used to the old mysql functions, trying out the mysqli and everything is a bit different.

Alan @ CIT 01-24-2008 11:00 AM

Cool cool, glad you got it fixed :-)

Alan


All times are GMT. The time now is 09:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0