1

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>  
7
  • What's the error message, and how does the content in $_POST['Matric'] look? Commented Apr 9, 2013 at 10:38
  • wait .. i not put echo for error.. Commented Apr 9, 2013 at 10:40
  • i put : if ($result)echo 'Add success';else echo 'Add failed'; but nothing come out... Commented Apr 9, 2013 at 10:42
  • if ($result) { echo 'Add succes'; } else { echo 'Add failed'; } ? To see what actually happens you need to enable error_reporting(E_ALL); Commented Apr 9, 2013 at 10:44
  • addslashes() works only for string php.net/manual/en/function.addslashes.php Commented Apr 9, 2013 at 10:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks .. now data can be insert into db.. but when i click add more field the column show in below the button ... and the input that i type in Matric reset.
@amanmakmur Firstly I can see you have 2 <div id="text"> with same id which is incorrect. Give them different id's.
@amanmakmur Glad to help you. Please don't forget to accept my answer by clicking the checkmark to the left of it.
i have other problem .. insert the image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.