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 05-30-2005, 06:37 PM   #1 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default Submission form issues

I have created a MySQL DB, stuff, it contains two a table named xbox. There are multiple fields that contain Title, publisher, purchased, etc.

I can successfully insert the title into the DB, but when I try to submit multiple input fields (from the web form) it only adds the publisher, not the title.

Here's the code:
PHP Code:
// If info has been submitted add it to the DB.
if (isset($_POST['title'])) {
 
$title $_POST['title'];
 
$sql "INSERT INTO xbox SET
  title='
$title'";
 }
 
if (isset(
$_POST['publisher'])) {
 
$publisher $_POST['publisher'];
 
$sql "INSERT INTO xbox SET
  publisher='
$publisher'";
 if (@
mysql_query($sql)) {
  echo 
'<p>Your info has been added.</p>';
 } else {
  echo 
'<p>Error adding submitted info: ' mysql_error(). '</p>';
 }

__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 05-30-2005, 08:00 PM   #2 (permalink)
The Acquainted
 
Join Date: Mar 2005
Posts: 177
Thanks: 0
CreativeLogic is on a distinguished road
Default

The way you're doing it will actually create two new rows. What you need to do instead is make that into one query so that they are inserted on the same row. Let me know if you need a further explaination.
CreativeLogic is offline  
Reply With Quote
Old 05-30-2005, 08:55 PM   #3 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

Yes, please explain, I'm not sure of any other way on how to do htis.

Thanks,
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 05-30-2005, 09:13 PM   #4 (permalink)
The Acquainted
 
Join Date: Mar 2005
Posts: 177
Thanks: 0
CreativeLogic is on a distinguished road
Default

This code is untested. If you don't understand what a part of it is doing just ask. It's fairly simple, but if you run into problems try looking up the php function first.
PHP Code:
// check for variables to contain data
if (!empty($_POST['title']))
{
    
// add values to array for later use
    
$row[] = 'title';
    
$value[] = "'" addslashes($_POST['title']) . "'";
}
if (!empty(
$_POST['publisher']))
{
    
$row[] = 'publisher';
    
$value[] = "'" addslashes($_POST['publisher']) . "'";
}

// check to see if we need to insert data
if (count($row) > 0)
{
    
// create query
    // the implode functions will create values in the query and if both
    // values are set will create a query looking like this:
    // INSERT INTO xbox (title, publisher) VALUES ('Title Posted Value', 'Publisher Posted Value')
    
$sql "INSERT INTO xbox (" implode(','$row) . ") VALUES (" implode(','$value) . ")";
    if (@
mysql_query($sql))
    {
        echo 
'<p>Your info has been added.</p>';
    }
    else
    {
        echo 
'<p>Error adding submitted info: ' mysql_error(). '</p>';
    }
}
else
{
    
// no data inserted

CreativeLogic is offline  
Reply With Quote
Old 06-01-2005, 07:33 PM   #5 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

a few additions to Ryan's code
at the top do this
$row =$value = array();//initialize just to avoid some notices which might come up later on.

and then in insert statement
$sql = "INSERT INTO xbox (" . implode(',', $row) . ") VALUES ('" . implode('','', $value) . "')";

just updated to add '(single quotes) around the values
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-01-2005, 11:55 PM   #6 (permalink)
The Acquainted
 
Join Date: Mar 2005
Posts: 177
Thanks: 0
CreativeLogic is on a distinguished road
Default

You're exactly correct with the query, and yes it is always better to initiate your variables but I have never got into this practice with PHP.
CreativeLogic 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form validation help 3gg1 General 0 10-06-2006 03:16 PM
Form Processing William Tips & Tricks 8 04-17-2005 03:24 PM
Form Processing Help Veolus Absolute Beginners 3 04-16-2005 04:16 AM


All times are GMT. The time now is 07:41 PM.

 
     

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