hello
i want if enter account save in database :)
this is my code , but it's not save anything in database :"(
i don't know why !
i have database "accounts" have "2 row name & account"

<html>
<body>
<?php

$con=mysql_connect("localhost","root","");  

  if(!$con)  
  {  
   die('can not connect'.mysql_error());  
  } 
  mysql_select_db("scms", $con ); 

   ?>

    <form action='' method='POST'>
        <table BORDER="0">
            <tr>
                <td>facbook account :</td>
                <td><input type="text" name="facebook" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save1" value="save"/></td>

              </tr>

              <tr>
                <td>twitter account :</td>
                <td><input type="text" name="twitter" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save2" value="save"/></td>
                 </tr>
                 </table>
                   </form>
                <?php

if(name=="save1")
{
$accout_f = $_POST[facebook];

 mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_f') WHERE name=facebook");

}
elseif(name=="save2")
{
    $accout_t = $_POST[twitter];

   mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_t') WHERE name=twitter");
 }
?>








</body>
</html>

Recommended Answers

All 16 Replies

Member Avatar for diafol

This should work

if(isset($_POST['name']) && $_POST['name']=="save1")
{
    $accout_f = mysql_real_escape_string($_POST['facebook']);
    mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_f') WHERE name='facebook'");
}
    elseif(isset($_POST['name']) && $_POST['name']=="save2")
{
    $accout_t = mysql_real_escape_string($_POST['twitter']);
    mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_t') WHERE name='twitter'");
 }

but it's not the way that I'd go about it. Your form means that you can only add one field at a time. It may be better if an user could add both on a single button click.

thank you , but it's not work

<html>
<body>
<?php

$con=mysql_connect("localhost","root","");  

  if(!$con)  
  {  
   die('can not connect'.mysql_error());  
  } 
  mysql_select_db("scms", $con ); 

   ?>

    <form action='' method='POST'>
        <table BORDER="0">
            <tr>
                <td>facbook account :</td>
                <td><input type="text" name="facebook" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save1" value="save"/></td>

              </tr>

              <tr>
                <td>twitter account :</td>
                <td><input type="text" name="twitter" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save2" value="save"/></td>
                 </tr>
                 </table>
                   </form>
  <?php

if(isset($_POST['name']) && $_POST['name']=="save1")
{   
 $accout_f = mysql_real_escape_string($_POST['facebook']);
 mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_f') WHERE name='facebook'");
 }    
 elseif(isset($_POST['name']) && $_POST['name']=="save2")
 {    $accout_t = mysql_real_escape_string($_POST['twitter']); 
    mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_t') WHERE name='twitter'");
}
?>


</body>
</html>
Member Avatar for diafol

OK, my mistake:

if(isset($_POST['save1']))
{
$accout_f = mysql_real_escape_string($_POST['facebook']);
mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_f') WHERE name='facebook'");
}
elseif(isset($_POST['save2']))
{
$accout_t = mysql_real_escape_string($_POST['twitter']);
mysql_query("INSERT INTO  accounts (account) VALUES ('$accout_t') WHERE name='twitter'");
 }

not save anything :""(

Member Avatar for diafol

Have you echoed anything in the code to see where you're going wrong?

e.g. echo $_POST['facebook'];etc?

Also you you just use an 'insert and go'. Add some error handling.

i try this code

<html>
<body>
<?php

$con=mysql_connect("localhost","root","");  

  if(!$con)  
  {  
   die('can not connect'.mysql_error());  
  } 
  mysql_select_db("scms", $con ); 
   ?>

    <form action='' method='POST'>
        <table BORDER="0">
            <tr>
                <td>facbook account :</td>
                <td><input type="text" name="facebook1" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save1" value="save"/></td>

              </tr>

                <tr>
                <td>twitter account :</td>
                <td><input type="text" name="twitter1" maxlength="50"/></td>
                <td><input type="SUBMIT" name="save2" value="save"/></td>

              </tr>

                 </table>
                </form>   
  <?php
  echo $_POST['facebook1'];
   echo $_POST['twitter1'];
  ?>

</body>
</html>

this output
error :(

Member Avatar for diafol

This will happen before you submit the form as the 'sent' vars don't exist yet. Does it still happen after submit?

if i write this

$a=$_POST['facebook1'];

 $sql="INSERT INTO  $accounts (account) VALUES ('$a') WHERE name='facebook'";

it's not work

but if write this

 $a=$_POST['facebook1'];

 $sql="INSERT INTO  $accounts (name,account) VALUES ('aaa','$a')";

it's work

why? :"(

Hi,

I think you need to focus more on validating which submit buttons were click. You have two of them. If I click one only, then on POST, you will get the undefined index error on the other.

Why not try validating your form submission based on the submit button clicks. for example

  if((isset($_POST['save1'])) || (isset($_POST['save']))){

   ## check here which one is not empty, and then prepare it for the database inclusion or posting.


  }

Please forgive my vigorous use of parenthesis. I can't just help it and it is becoming more of a second nature to me overtime, it is probably because of my mathematics background and calculator projects I have been working on and in the past. In mathematics, extra care is needed in isolating integers and variables, so that the program will calculate based on the PEMDAS convention. Otherwise, the calculated output is inaccurate, and others will not be able to replicate it.

problem it's here

WHERE name='facebook' 
Member Avatar for diafol

You don't need a Where clause on an insert query. It doesn't make sense to do so in your example

no, i need if enter account in facebook save in account if name == facebook

i don't know what is problem here
$sql="INSERT INTO $accounts (account) VALUES ('$facebook') WHERE name='facebook'";

if i write same this
$sql="INSERT INTO $accounts (name,account) VALUES ('fgfgfg','$facebook') ";
it's work !!!!

Member Avatar for diafol

Like I said WHERE clause doesn't belong in INSERT query. The WHERE clause is usually linked to SELECT, UPDATE and DELETE. As you are inserting, the WHERE clause has no meaning - not in this context anyway.

ok , i will try with updata

it's work , use update

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.