How to save data on MySQL from PHP script?

Asked By 10 points N/A Posted on -
qa-featured

I don't get errors or anything but it doesn't save in my db?

 

Please help.

<?php>

if(isset($_POST[save]))

{

//student,grade,section,subject

$ids=$_POST['id'];

$grades=$_POST['fg'];

$secs=$_POST['secs'];

$subs=$_POST['subs'];

$yirs=$g_year['acad_id'];

 

debug($_POST);

if(isset($_POST['save']))

{

 

unset($_POST['save']);

$i = count($_POST['id']); $x = 0;

 

for($x=0; $x <$i;$x++){

foreach($_POST as $data =>$d){

echo $data.'-'.$d[$x].'<br/>';

 

$num_entry=count($d);

 

$insert="INSERT INTO tbl_firstgrading {$data} VALUES (";

for($i=0;$i<$num_entry;$i++){

if($i!=$num_entry){

$insert.="$d,";

}

else

{

$result=mysql_query($insert)or die(mysql_error());

}

}

//SQL statement here (example): insert into table name (value ng $data) values( value ng $d[$x]) —

}}}}

?> <?php function debug($arr){ echo "

 

";

print_r($arr);

echo "

"; } ?>

SHARE
Best Answer by Estillero State
Best Answer
Best Answer
Answered By 5 points N/A #118687

How to save data on MySQL from PHP script?

qa-featured

Hi Apollo,

I examined your code and it looked fine and working except for one thing. I can't see the part where you open connection to the database. It must be somewhere here, unless you put it somewhere not included in the source code you put here.

// connect to your database here

$con = mysql_connect("localhost", "you_user_name", "your_password");

if(!$con)

{

die('Could not connect:'. mysql_error());

}

// select your database here

mysql_select_db("you_db", $con);

// after this block your connection must be valid

 

$insert="INSERT INTO tbl_firstgrading {$data} VALUES (";
for($i=0;$i<$num_entry;$i++){
if($i!=$num_entry){
$insert.="$d,";
}
else
{
$result=mysql_query($insert)or die(mysql_error());
}
}
// dont forget to close connection
mysql_close($con);
 
I hope it helps.
Answered By 5 points N/A #118689

How to save data on MySQL from PHP script?

qa-featured

Hi there user:

Create using this Basic html save and update.

<html>

<head>

<title>update database</title>

<head>

<body>

<form method=”post” action=”update.php”>

</body>

</html>

And then add three (3) input boxes for three (3) column in your database. FName, LName, and then PHON. This is how you’ll update or save your database.

Hope this could help you. By the way, the fourth (4th) column is auto-generated, this one doesn’t need to update.

Thank you for posting here at techyv.com

Best regards.

 

Related Questions