aman makmur
aman makmur

Reputation: 35

insert php for array fail

i have problem. when i submit nothing insert to mysql database. The matric can be same with the different certificate name.Actually, i try used an array but i fail to configure it... please help me

<html>
<head>
<script language="javascript">
fields = 0;
function addInput() {
if (fields != 10) {
document.getElementById('text').innerHTML += "<br/><input type='text' name='cer[]' size='30' />";

fields += 1;

} else {
document.getElementById('text').innerHTML += "<br />Maximum 10 fields allowed.";
document.form.add.disabled=true;
}
}
</script>
</head>
<body>
<form action="" method="post"> 
<div id="text"><p>matric</p> 
  <input type="text" name="matric" size="30" /> 
 <div id="text"><p>Certificate name</p> 
  <input type="text" name="cer[]" size="30" /> 
  <br /> 
 <input type="button" onclick="addInput()" name="add" value="Add input field" />
 <input type="submit" value="Submit" name="sumbit"/>
 </div> 
</form> 
<?php
include 'dbconnect.php';
if (isset($_POST['submit'])){

$Matric = addslashes($_POST['Matric']);
$Cer_name = addslashes($_POST['cer[]']);

if(!empty($Cer_name)){
$query = "INSERT INTO certificate (Matric,Cer_name) VALUE ('$Matric','$Cer_name')";
$result = mysql_query($query);
}
}
?>
</body>
</html>  

Upvotes: 1

Views: 115

Answers (1)

Rikesh
Rikesh

Reputation: 26451

I guess column Cer_name in your db table must be of type varchar. And you are try to store complete array. So better way is try to save certificates as comma separated.

$Cer_name = addslashes(implode(',', $_POST['cer']));
$query = "INSERT INTO certificate (Matric,Cer_name) VALUE ('$Matric','$Cer_name')";
$result = mysql_query($query) or die(mysql_error());

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Upvotes: 1

Related Questions