0

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>
3
Contributors
16
Replies
18
Views
5 Years
Discussion Span
Last Post by don't give up
0

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.

0

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>
0

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'");
 }
0

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.

0

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 :(
121212

0

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

0

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? :"(

0

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.

Edited by veedeoo: more info added

0

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

0

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 !!!!

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.