Hi all,

I just know whether it will allow in php to store data in a sigle form into 2 tables, because i want to use these data in some other tasks.

In my form there are 7 fields. They are nic, full name, name with initials, phone number, address, gender, date of birth, account type and fd period

I want to include nic, full name, name with initials, phone number, address, gender, dob into my "account_details" table

And, i want to include account_type and fd_period to my "account" table

The 2 tables look like this.

account_details
(account_number, nic, full_name, name_with_initials, phone_number, address, gender, dob)

account (account_number, account_type, fd_period, account_balance, account_interest)

Recommended Answers

All 21 Replies

Member Avatar for diafol

yes

yes,

edit, gday ardav, & 'scuse me hadnt seen the prior answer

Member Avatar for diafol

no prob AB. Nice to see you back and at your best following your lengthy soujourn :)

If so what's the wrong with this coding??

<form id="form1" name="form1" method="post" action="">
  <label>
  <input type="submit" name="button" id="button" value="Home" />
  </label>
</form>
<p>&nbsp;  </p>
<p>
  <?php
$connect=mysql_connect('localhost','root','');
mysql_select_db('bank',$connect);
//Create Array
$info = array(
           'nic' => $_POST["nic"],
           'full_name' => $_POST["full_name"],
           'name_with_initials' => $_POST["name_with_initials"],
           'phone_number' => $_POST["phone_number"],
           'address' => $_POST["address"],
           'gender' => $_POST["gender"],
           'date_of_birth' => $_POST["date_of_birth"],
	  'account_type' => $_POST["account_type"],
	  'fd_period' => $_POST["fd_period"]
                    
           );
 //Prepare the Insert Query
$insert_query  = "INSERT INTO account_details (
               nic,
               full_name,
               name_with_initials,
               phone_number,
               address,
               gender,               
               date_of_birth
			   ) 
               VALUES
               (
               '$info[nic]', 
               '$info[full_name]', 
               '$info[name_with_initials]', 
               '$info[phone_number]', 
               '$info[address]', 
               '$info[gender]', 
	      '$info[date_of_birth]'
			         
               )";
			   
$atype = mysql_real_escape_string($info['account_type']); //assuming this is a string
$fdper = mysql_real_escape_string($info['fd_period']); //assuming this is a string
$anum = intval($info['account_number']); //assuming this is an integer

$query = "UPDATE account SET `account_type` = '$atype', `$fd_period` = '$fdper' WHERE `account_number`= '$anum'";mysql_query($query) or die(mysql_error());			   

               	   
//Run a switch on the chosen type of account
if($info['account_type'] == "abhimani_plus") {
      if($info['gender']!="female") {
         //Show error messages here
         echo "You do not meet the critera required to open this account.";exit;
      }
   }
 //Account Creation Here
$r = mysql_query ($insert_query);
if($r) {
 echo "A new account with number ".mysql_insert_id()." has been created successfully.";die();
}

?>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

it is safer and cleaner to separate PHP section/query. Therefore, try putting all PHP codes into a PHP file. Then, name it, for example query.php, then put the name into the action attribute of your form. In regard to adding/updating data on your site, I can NOT see any fields in your form except the submit button. OK, here is a simple way of doing this, in your form where you have the input fields, have an attribute

name = "nic"

then in your PHP file. Call it to get the data from users like

$nic = $_POST['nic']

I hope this helps.

Build the require fields in the form. PHP is also ok. You need to put the fields that would process by PHP.

$info = array(
'nic' => $_POST["nic"],
'full_name' => $_POST["full_name"],
'name_with_initials' => $_POST["name_with_initials"],
'phone_number' => $_POST["phone_number"],
'address' => $_POST["address"],
'gender' => $_POST["gender"],
'date_of_birth' => $_POST["date_of_birth"],
'account_type' => $_POST["account_type"],
'fd_period' => $_POST["fd_period"]
);

You must have fields for the above array.

Yeah i have fields for the above array.

I also tried this coding

<?php
$connect=mysql_connect('localhost','root','');
mysql_select_db('bank',$connect);

$nic = $_POST['nic'];
$full_name = $_POST['full_name'];
$name_with_initials = $_POST['name_with_initials'];
$phone_number = $_POST['phone_number'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$date_of_birth = $_POST['date_of_birth'];
$account_type = $_POST['account_type'];
$fd_period = $_POST['fd_period'];

$insert_query = "INSERT INTO account_details (
                            nic,
			    full_name,
			    name_with_initials,
			    phone_number,
			    address,
			    gender,
			    date_of_birth
			    )
			    VALUES
			    (
                            '$nic',
			    '$full_name',
			    '$name_with_initials',
			    '$phone_number',
			    '$address',
			    '$gender',
			    '$date_of_birth'
			    )";
			   
						   
           	   
//Run a switch on the chosen type of account
if($_POST['account_type'] == "abhimani_plus") {
      if($_POST['gender']!="female") {
         //Show error messages here
         echo "You do not meet the critera required to open this account.";exit;
      }
   }
 //Account Creation Here
$r = mysql_query ($insert_query);
if($r) {
 echo "A new Account number ".mysql_insert_id()." has been created successfully.";die();
}

 $query = "UPDATE account SET 'account_type' = '$account_type', 'fd_period' = '$fd_period' WHERE 'account_number' = '$account_number'";

mysql_query($query) or die(mysql_error());
	

?>

It only insert data into my account_details table.The successfull message also displayed. But NOTHING will happen to my "account" table.It does not get updated..

$query = "UPDATE account SET 'account_type' = '$account_type', 'fd_period' = '$fd_period' WHERE 'account_number' = '$account_number'";

Replace with

$query = "UPDATE account SET `account_type` = '$account_type', `fd_period` = `$fd_period` WHERE `account_number` = '$account_number'";

It is not quote. It is groove left side of the numeric key 1.

It is the same code that i have used earlier..

Not same like yours.

SET `account_type` = '$account_type'

Use (``) symbol left side of the numeric key 1 for the field name. Use quote ('') for the value.

done. but it does not work. It generates an error. The data does not pass to any table.. :-(

What error ?

It says,

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\MySite\php files\handlers\account.php on line 72

Show that line 72 in account.php.

I have used both these codings. But similar error displays.

$query = "UPDATE account SET "account_type" = '$account_type', "fd_period" = '$fd_period' WHERE "account_number" = '$account_number'";
$query = "UPDATE account SET 'account_type' = "$account_type", 'fd_period' = "$fd_period" WHERE 'account_number' = "$account_number"";

As I mentioned above, it is not quote ('account_type', 'fd_period', 'account_number').
It is groove symbol that is left side of the numeric key 1, mostly known as tilde key.
Copy below and paste in your file:

$query = "UPDATE account SET `account_type` = '$account_type', `fd_period` = '$fd_period' WHERE `account_number` = '$account_number'";

Use single-quote ('') for the value. If not, escape them with slash (\"\").
Hope this help.

nothing will happen to your account table its becasue you update the record that doesnt exist.

insert record before update

Show us your HTML form( all fields) please,,,

@ happytogether,

That is my proble. If i try to put the INSERT query only that one will execute. I want to insert records for both of the tables.

This is my HTML form.

<?php if(isset($_POST['run'])){include_once "./handlers/account.php";} ?>
<form action="open_account.php" name="form1" method="post">
<input type="hidden" name="run" value="yes" />
  <fieldset>
  <legend class="cap">Customer details</legend>
  <table width="75%" border="0" cellspacing="0" cellpadding="5" align="center">
                 <tr>
                  <td>&nbsp;</td>
                  <td class="title02">&nbsp;</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                 </tr>
                                          
                 <tr height="30">
                  <td width="10%">&nbsp;</td>


                  <td width="25%" class="title02" align="left">NIC</td>
                  <td width="55%" class="attribute1" align="left"><label>
                    <input type="text" name="nic" id="textfield" />
                  </label></td>
                  <td width="10%">&nbsp;</td>
                 </tr>
                 <tr height="30">
                  <td>&nbsp;</td>
                        <td width="25%" class="title02" align="left">Full name</td>
                    <td width="55%" class="attribute1" align="left"><label>
              <input type="text" name="full_name" class="attribute1" />
              </label></td>
                  <td width="10%">&nbsp;</td>
                 </tr>
                 <tr height="30">
                  <td>&nbsp;</td>
                  <td class="title02" align="left">Name with initials</td>
                  <td class="attribute1" align="left"><input type="text" name="name_with_initials" class="attribute1" /></td>
                 </tr>
                   <tr height="30">
                  <td width="10%">&nbsp;</td>


                  <td width="25%" class="title02" align="left">Phone Number</td>
                  <td width="55%" class="attribute1" align="left"><label>
                  <input type="text" name="phone_number" class="attribute1" />
                  </label></td>
                  <td width="10%">&nbsp;</td>
                 </tr>
                       <tr height="30">
                  <td width="10%">&nbsp;</td>


                  <td width="25%" class="title02" align="left">Address</td>
                  <td width="55%" class="attribute1" align="left"><label>
                    <textarea name="address" id="textarea" cols="45" rows="5"></textarea>
                  </label></td>
                  <td width="10%">&nbsp;</td> 
    <tr height="30">
                        
                  <td>&nbsp;</td>
                  <td class="title02" align="left">Gender</td>
                  <td class="attribute1" align="left"><label>
                    <select name="gender" id="select">
                           <option selected="selected"></option>
                      <option value="male">Male</option>
                            <option value="female">Female</option>
                     </select>
                  </label></td>
    <tr height="30">
                  <td>&nbsp;</td>
                  <td class="title02" align="left">Date of birth</td>
                  <td class="attribute1" align="left"><input type="Text" id="demo3" name="date_of_birth" maxlength="25" size="25"/><img src="../images/cal.gif" onClick="javascript:NewCssCal('demo3','yyyyMMdd')" style="cursor:pointer"/>&nbsp;</td>
    </tr>
     
      
      <tr height="30">
        <td>&nbsp;</td>
        <td width="25%" class="title02" align="left">Account Type</td>
        <td width="55%" align="left" bgcolor="#FFFFFF" class="attribute1">
        <select name="account_type" onChange="fd_show(this.value)">
            <option selected="selected"></option>
            <option value="savings_investment">Savings Investment</option>
            <option value="shakthi" >Shakthi</option>
            <option value="surathal">Surathal</option>
            <option value="abhimani_plus">Abhimani Plus</option>
            <option value="yasasa">Yasasa Certificates</option>
            <option value="fd">Fixed Deposits</option>
          </select>&nbsp;</td>
     
      <tr height="30">
        <td colspan="4">
            <div id="fd_box" style="visibility: hidden;">
            <table width="100%" border="0" cellspacing="0" cellpadding="5" align="center">
            <tr height="30">
                   
            </tr>
            <tr height="30">
              <td width="10%">&nbsp;</td>
              <td width="25%" class="title02" align="left">FD period</td>
              <td width="55%" class="attribute1" align="left"><select name="fd_period">
                 <option selected="selected"></option>
                  <option value="< 1">less than 1 year</option>
                  <option value="1-3 years" >1-3 years</option>
                  <option value="> 3">more than 3 years</option>
                  <option value="il">immediate loan</option>
                </select></td>
              <td width="10%">&nbsp;</td>
            </tr>
            </table>
            </td>
      </tr>
  </table>
    <p align="center">&nbsp;</p>
    <p align="center">
      <input type="submit" onClick="return Validate();" name="submit" value="Submit" class="attribute1" />
      &nbsp;&nbsp;
      <input type="reset" name="reset" value="Reset" class="attribute1" />
      &nbsp;&nbsp;
      <label>
        <input type="submit" name="button2" id="button2" value="Help" />
      </label>
    </p>
  </fieldset>
  </td>
  <td width="5%">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><font color="red" size="1" ></font></td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>

insert into table1 dataset1
insert into dable2 dataset2

reiterating what has been said before (shout) IT IS NOT POSSIBLE (/shout) to update a record that does not exist, update is not a synonym of insert

Now it's ok. Earlier i tried 2 INSERT statements but did not work. That's why i asked.

Anyway, thanks everyone who help me out regarding this matter. Thanks again..:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.