TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-24-2008, 09:06 AM   #1 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Bug 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>
d4v1d is offline  
Reply With Quote
Old 01-24-2008, 10:03 AM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-24-2008, 10:29 AM   #3 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 01-24-2008, 10:55 AM   #4 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

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.
d4v1d is offline  
Reply With Quote
Old 01-24-2008, 11:00 AM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Cool cool, glad you got it fixed

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design