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
https://forums.phpfreaks.com/topic/102831-matrik/
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
https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526772
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
https://forums.phpfreaks.com/topic/102831-matrik/#findComment-526800
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.