Jump to content

Matrik


june_c21

Recommended Posts

i try to run this code but the result is INSERT INTO list (name,age) VALUES ('', '') how to make it insert to the database??

 

my html code

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Name</title>
</head>

<body>

<form method="POST" action="index2.php">
<!--webbot bot="SaveResults" U-File="C:\wamp\www\_private\form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<table border="1" width="100%">
	<tr>
		<td>Name</td>
		<td>Age</td>
	</tr>
	<tr>
		<td><input type="text" name="name1" size="20"></td>
		<td><input type="text" name="age1" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name2" size="20"></td>
		<td><input type="text" name="age2" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name3" size="20"></td>
		<td><input type="text" name="age3" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name4" size="20"></td>
		<td><input type="text" name="age4" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name5" size="20"></td>
		<td><input type="text" name="age5" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name6" size="20"></td>
		<td><input type="text" name="age6" size="20"></td>
	</tr>
	<tr>
		<td><input type="text" name="name7" size="20"></td>
		<td><input type="text" name="age7" size="20"></td>
	</tr>
</table>
<p> </p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

 


<?php
session_start();
$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'project';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$name    = $_POST['name'];
$age = $_POST['age'];

if (strlen('$name')> 0 ){ 
     
$query= "INSERT INTO list (name,age) VALUES ('$name', '$age')";
echo $query;
$result = mysql_query($query,$dblink);
}

?>

 

 

 

please help me. i m new with this. thanks

Link to comment
Share on other sites

Don't bump so quickly. People will help, it just doesn't come instantly.

 

<?php
session_start();
$host = 'localhost';
$user = 'root';
$password = '';
$dbase = 'project';

$dblink = mysql_connect($host,$user,$password);
mysql_select_db($dbase,$dblink);

$query= "INSERT INTO list (name,age) VALUES ";

for($i=1; $i<8; $i++){
 $name = $_POST['name' . $i];
 $age = $_POST['age' . $i];
 if(strlen($name) > 0){
   $values[] = array('name'=>$name, 'age'=>$age);
 }
}

foreach($values as $key=>$value){
$query .= "('{$value['name]}', '{$value['age']}')";
if($key != 7){
 $query .= ",\n";
}
}

echo $query;
$result = mysql_query($query,$dblink);
?>

 

(Not tested)

Link to comment
Share on other sites

Change

  $name = $_POST['name' . $i];

  $age = $_POST['age' . $i];

  if(strlen($name) > 0){

    $values[] = array('name'=>$name, 'age'=>$age);

  }

 

to

 

  $name = $_POST['name' . $i];

  $age = $_POST['age' . $i];

  if(strlen($name) > 0 && strlen($age) > 0){

    $values[] = array('name'=>$name, 'age'=>$age);

  }

 

See if that works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.