Current location: Hot Scripts Forums » Programming Languages » PHP » MySql update form script?


MySql update form script?

Reply
  #1 (permalink)  
Old 01-09-04, 10:23 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
MySql update form script?

Have anyone got a simple form script that can be used to input data to a table on MySql?

i.e

name
address
age
sex


MySql Table-

Name--Address-------Age-----Sex
john---22 smith rd----29------male
david--21 smith rd----22------male


PHP Code:

<?PHP

// Connect to database
$link mysql_connect('localhost','user''password')
    or die(
"Could not connect");
mysql_select_db('select_db')
    or die(
"Could not select database");

 
//?????


// Content
echo '<body bgcolor="#155271" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<form name='
guestlist' method='post'>
  <TABLE width="395" height="119" border=0 cellPadding=3 cellSpacing=2 bgcolor="#0F364A">
    <TBODY>
      <TR> 
        <TD class=mainText width="56"><b>Name</b></TD>
        <TD width="305" class=frmInput> <INPUT class=mainFormBox style="WIDTH: 320px" type="text" name="name"></TD>
      </TR>
      <TR> 
        <TD class=mainText width="56"><B>Address</B></TD>
        <TD class=frmInput> <INPUT name="address" type="text" class=mainFormBox id="address" style="WIDTH: 320px"></TD>
      </TR>
      <TR> 
        <TD class=mainText width="56"><b>Age</b></TD>
        <TD class=frmInput> <INPUT name="age" type="text" class=mainFormBox id="age" style="WIDTH: 320px"></TD>
      </TR>
            <TR> 
        <TD class=mainText width="56"><b>Sex</b></TD>
        <TD class=frmInput> <INPUT name="sex" type="text" class=mainFormBox id="sex" style="WIDTH: 320px"></TD>
      </TR>
      <TR> 
        <TD class=frmDescription width="56">&nbsp;</TD>
        <TD class=frmInput> <div align="right"> 
            <INPUT type=image name="submit" value="Submit" height=20 width=109 src="sendrequest.jpg" align=Submit >
          </div></TD>
      </TR>
    </TBODY>
  </TABLE>
</form>
  </body>'
;

mysql_close();
?>

Last edited by dihan; 01-09-04 at 10:33 AM.
Reply With Quote
  #2 (permalink)  
Old 01-09-04, 10:39 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
PHP Code:

<?php

if (!isset($_POST['submit'])) {
 echo
'<body bgcolor="#155271" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<form name="guestlist" method="POST" action="./guestlist.php">
  <TABLE width="395" height="119" border=0 cellPadding=3 cellSpacing=2 bgcolor="#0F364A">
    <TBODY>
      <TR>
        <TD class=mainText width="56"><b>Name</b></TD>
        <TD width="305" class=frmInput> <INPUT class=mainFormBox style="WIDTH: 320px" type="text" name="name"></TD>
      </TR>
      <TR>
        <TD class=mainText width="56"><B>Address</B></TD>
        <TD class=frmInput> <INPUT name="address" type="text" class=mainFormBox id="address" style="WIDTH: 320px"></TD>
      </TR>
      <TR>
        <TD class=mainText width="56"><b>Age</b></TD>
        <TD class=frmInput> <INPUT name="age" type="text" class=mainFormBox id="age" style="WIDTH: 320px"></TD>
      </TR>
            <TR>
        <TD class=mainText width="56"><b>Sex</b></TD>
        <TD class=frmInput> <INPUT name="sex" type="text" class=mainFormBox id="sex" style="WIDTH: 320px"></TD>
      </TR>
      <TR>
        <TD class=frmDescription width="56">&nbsp;</TD>
        <TD class=frmInput> <div align="right">
        <input type="hidden" name="submit" value="done" />
            <INPUT type=image name="submit" value="Submit" height=20 width=109 src="sendrequest.jpg" align=Submit >
          </div></TD>
      </TR>
    </TBODY>
  </TABLE>
</form>
  </body>'
;
//note that I added a new field called submit and it's hidden ..

} else {

/*I added addslashes() so it escape any special char in the form ..
if you want to show your inserted record , you need to use stripslashes() to remove any slashes from the record ..
this is only to avoid some security problems in the query ..*/

$name=trim(addslashes($_POST['name']));
$address=trim(addslashes($_POST['address']));
$age=trim(addslashes($_POST['age']));
$sex=trim(addslashes($_POST['sex']));

$link mysql_connect('localhost','select_user''password')
    or die(
"Could not connect");
mysql_select_db('select_db')
    or die(
"Could not select database");

$insert=mysql_query("INSERT INTO table (name, address, age, sex)
VALUES('
$name', '$address', '$age', '$sex')")or
die(
'couldn\'t Insert record into database :'.mysql_error());

echo
'The Record was inserted succesfully into Database';
}
?>
good luck
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 01-09-04 at 10:47 AM.
Reply With Quote
  #3 (permalink)  
Old 01-09-04, 11:07 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Hey I tried out this: but no luck I thought it may have somthing to do with "<form name='guestlist' method='post'>" cause of the " ' ". Could that be it?

http://www.selectguestlist.com/index/update.php


PHP Code:

 <?PHP 

 
if (!isset($_POST['submit'])) { 
echo
'<body bgcolor="#155271"> 
<LINK href="home/style.css" type=text/css rel=stylesheet>
<form name='
guestlist' method='post'> 
  <TABLE width="395" height="119" border=0 cellPadding=3 cellSpacing=2 bgcolor="#0F364A"> 
    <TBODY> 
      <TR> 
        <TD class=mainText width="56"><b>Name</b></TD> 
        <TD width="305" class=frmInput> <INPUT class=mainFormBox style="WIDTH: 320px" type="text" name="name"></TD> 
      </TR> 
      <TR> 
        <TD class=mainText width="56"><B>Address</B></TD> 
        <TD class=frmInput> <INPUT name="address" type="text" class=mainFormBox id="address" style="WIDTH: 320px"></TD> 
      </TR> 
      <TR> 
        <TD class=mainText width="56"><b>Age</b></TD> 
        <TD class=frmInput> <INPUT name="age" type="text" class=mainFormBox id="age" style="WIDTH: 320px"></TD> 
      </TR> 
            <TR> 
        <TD class=mainText width="56"><b>Sex</b></TD> 
        <TD class=frmInput> <INPUT name="sex" type="text" class=mainFormBox id="sex" style="WIDTH: 320px"></TD> 
      </TR> 
      <TR> 
        <TD class=frmDescription width="56">&nbsp;</TD> 
        <TD class=frmInput> <div align="right"> 
            <INPUT type=image name="submit" value="Submit" height=20 width=109 src="guestlist/sendrequest.jpg" align=Submit > 
          </div></TD> 
      </TR> 
    </TBODY> 
  </TABLE> 
</form> 
  </body>'

} else { 

$name=trim(addslashes($_POST['name'])); 
$address=trim(addslashes($_POST['address'])); 
$age=trim(addslashes($_POST['age'])); 
$sex=trim(addslashes($_POST['sex'])); 

$link mysql_connect('localhost','select_user''select_password'
    or die(
"Could not connect"); 
mysql_select_db('select_db'
    or die(
"Could not select database"); 

$insert=mysql_query("INSERT INTO table (name, address, age, sex) 
VALUES('
$name', '$address', '$age', '$sex')"); 

$affected=mysql_affected_rows(); 

echo 
$affected .' Record was inserted into Database'


?>
Reply With Quote
  #4 (permalink)  
Old 01-09-04, 11:34 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
opps I forgot something very important ..
try this one now :

PHP Code:

<?php

    
if (!isset($_POST['submit'])) {
    echo
'<body bgcolor="#155271" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
    <form name="guestlist" method="POST" action="'
.$_SERVER['PHP_SELF'].'">
      <TABLE width="395" height="119" border=0 cellPadding=3 cellSpacing=2 bgcolor="#0F364A">
        <TBODY>
          <TR>
            <TD class=mainText width="56"><b>Name</b></TD>
            <TD width="305" class=frmInput> <INPUT class=mainFormBox style="WIDTH: 320px" type="text" name="name"></TD>
          </TR>
          <TR>
            <TD class=mainText width="56"><B>Address</B></TD>
            <TD class=frmInput> <INPUT name="address" type="text" class=mainFormBox id="address" style="WIDTH: 320px"></TD>
          </TR>
          <TR>
            <TD class=mainText width="56"><b>Age</b></TD>
            <TD class=frmInput> <INPUT name="age" type="text" class=mainFormBox id="age" style="WIDTH: 320px"></TD>
          </TR>
                <TR>
            <TD class=mainText width="56"><b>Sex</b></TD>
            <TD class=frmInput> <INPUT name="sex" type="text" class=mainFormBox id="sex" style="WIDTH: 320px"></TD>
          </TR>
          <TR>
            <TD class=frmDescription width="56">&nbsp;</TD>
            <TD class=frmInput> <div align="right">
            <input type="hidden" name="submit" value="done" />
                <INPUT type=image name="submit" value="Submit" height=20 width=109 src="sendrequest.jpg" align=Submit >
              </div></TD>
          </TR>
        </TBODY>
      </TABLE>
    </form>
      </body>'
;
    
//note that I added a new field called submit and it's hidden ..

    
} else {

    
/*I added addslashes() so it escape any special char in the form ..
    if you want to show your inserted record , you need to use stripslashes() to remove any slashes from the record ..
    this is only to avoid some security problems in the query ..*/

    
$name=trim(addslashes($_POST['name']));
    
$address=trim(addslashes($_POST['address']));
    
$age=trim(addslashes($_POST['age']));
    
$sex=trim(addslashes($_POST['sex']));

    
$link mysql_connect('localhost','select_user''password')
        or die(
"Could not connect");
    
mysql_select_db('select_db')
        or die(
"Could not select database");

    
$insert=mysql_query("INSERT INTO table (name, address, age, sex)
    VALUES('
$name', '$address', '$age', '$sex')")or
    die(
'couldn\'t Insert record into database :'.mysql_error());

    echo
'The Record was inserted succesfully into Database';
    }
    
?>
it should redirct the posted into itself !! now this form submits the info to itself so you don't need any other file to do the process on the submitted info ..
this one should do

btw I tried the link you provided , after I submit why does it pass the vars in the URL ?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 01-09-04 at 11:39 AM.
Reply With Quote
  #5 (permalink)  
Old 01-09-04, 12:16 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Code:
couldn't Insert record into database :
You have an error in your SQL syntax near 'table (name, address, age, sex) VALUES('NeverMind', 'Makkah', '18', 'Male')' at line 1
you need to change 'table' to the real table name in the query ..
I've seen the error which comes after submition ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 01-09-04 at 12:24 PM.
Reply With Quote
  #6 (permalink)  
Old 01-09-04, 12:21 PM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
I called the table - table as it was a test but thats probably not a good idea. I'll just chnage it to something else
Reply With Quote
  #7 (permalink)  
Old 01-09-04, 12:26 PM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by NeverMind
Code:
couldn't Insert record into database :
You have an error in your SQL syntax near 'table (name, address, age, sex) VALUES('NeverMind', 'Makkah', '18', 'Male')' at line 1
you need to change 'table' to the real table name in the query ..
I've seen the error which comes after submition ..
I just did the Type as text. Could that be a problem


Field Type Attributes Null Default Extra Action
name text No Change Drop Primary Index Unique Fulltext
address text No Change Drop Primary Index Unique Fulltext
age text No Change Drop Primary Index Unique Fulltext
sex text No Change Drop Primary Index Unique Fulltext
Check All / Uncheck All With selected: Or
Reply With Quote
  #8 (permalink)  
Old 01-10-04, 02:44 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
I've seen that you'd removed the quote in the values ..
now try this query :
Code:
INSERT INTO table (name, Address, age, sex) VALUES ('$name', '$address', '$age', '$sex')
this is really strange ! I don't know what is the problem ..
maybe you didn't select the right database or the table !?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #9 (permalink)  
Old 01-10-04, 03:35 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Go it

OK dont call your table - "table" or "update", that was the problem.

Thanks for the help NeverMind, I think I'm going to put you/your site on the credits for this website. :-)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM
Looking for a update table script thebugz Script Requests 1 12-07-03 06:32 PM
live chat script, form script, autoresponder, babalu Script Requests 0 12-04-03 01:55 PM
PHP to MySQL script question...(using a field to update info in MySQL) DisneyFan25863 PHP 4 11-02-03 03:31 AM
PHP + MySQL script help need andyinorbit2000 PHP 1 09-09-03 02:07 AM


All times are GMT -5. The time now is 08:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.