Search Posts

Begginer-Can’t insert data into database through arrays and implode function using php mysql

how to – Begginer-Can’t insert data into database through arrays and implode function using php mysql

I am trying to insert an Html form data into my DB using PHP arrays and implode function.
The code works fine with no errors and gives me prompt stating values have been inserted. However, when I query into my DB the Values are not present.

Html Code:

<form action="##"  method="Post">

User Name

<input type ="text" name="uname" >

First Name

<input type ="text" name="fname"  >

Email

<input type ="email" name="email" >

submit

<input type ="submit" name="submit" value="submit">

PHP CODE :

Db Created

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

$db=mysql_select_db("abcd");

Values Passed via POST method

$a=mysql_real_escape_string($_POST['uname']);

$b=mysql_real_escape_string($_POST['fname']);

$c=mysql_real_escape_string($_POST['email']);

converting values into an array.

$data=array('$a','$b','$c');

Usage of implode function for inserting data in feild names in MySql.

$f_sql= "'" . implode(',' , array_keys($data)) ."'";

Usage of implode function for inserting data in field names. This will be used for inserting values in Mysql

$v_sql="'".implode ("' ,",$data)."'";

Finally MySql Command:

$sql="insert into pracproj1 ($f_sql) VALUES  ($v_sql)";

if( $sql== true){
    echo "updated";

}

else
    "failed";

I get what I echoed after I press the submit button. But as I said. Real-time values aren’t getting updated into my DB.

If, somehow I don’t use an array or Implode function I don’t get stuck anywhere.

I am trying to learn few more PHP concepts that I can use with my Html form and that’s why I started working with Implode and arrays.
Kindly help.

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *