Submit button not transfering data

Hello everyone, I started this week with php and I’m currently working on a php file which transfers data into an mysql-table. However, I can’t get it to work.

PHP

<html>
<head>
<title>IT Battenberg</title>
<link rel="stylesheet" href="style.php" media="screen">
</head>
<body>
<header>
   <div class="navbar">
	   <a href="index.html">Home</a>
	   <div class="dropdown">
		   <button class="drpbtn">Lernmaterial
			   <i class="fa fa-caret-down"></i>
		   </button>
		   <div class="dropdown-content">
			 <a href="#">Link 1</a>
			 <a href="#">Link 2</a>
			 <a href="#">Link 3</a>
		   </div>
	   </div>
	   <a href="process_form.php">Aufträge</a>
	   <div class="dropdown">
		   <button class="drpbtn">NAS
			   <i class="fa fa-caret-down"></i>
		   </button>
		   <div class="dropdown-content">
			 <a href="#">Link 1</a>
			 <a href="#">Link 2</a>
			 <a href="#">Link 3</a>
		   </div>
	   </div>
	   <div class="dropdown">
		   <button class="drpbtn">Über uns
			   <i class="fa fa-caret-down"></i>
		   </button>
		   <div class="dropdown-content">
			 <a href="#">Link 1</a>
			 <a href="#">Link 2</a>
			 <a href="#">Link 3</a>
		   </div>
	   </div>
   </div>
</header>	
	
	
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="horst"; // Database name
$tbl_name="vollhorst"; // Table name
// Connect to server and select databse.
$link = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");
$sql="SELECT id, name, auftrag, bemerkungen FROM $tbl_name";
$result=mysqli_query($link, $sql);
// Count table rows
$count=mysqli_num_rows($result);
?>
<table id="tbl">
<form name="form1" method="post" action="">
<tr class="row">
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr class="row">
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Auftrag</strong></td>
<td align="center"><strong>Bemerkungen</strong></td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
?>
<tr class="row">
<td align="center">
<?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="auftrag[]" type="text" id="auftrag" value="<?php echo $rows['auftrag']; ?>">
</td>
<td align="center">
<input name="bemerkungen[]" type="text" id="bemerkungen" value="<?php echo $rows['bemerkungen']; ?>">
</td>
</tr>
<?php
}
?>
<tr class="row">
<td colspan="4" align="center"><input id="sbt" type="submit" name="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
$name = isset($_POST['name']);
$auftrag = isset($_POST['auftrag']);
$bemerkungen = isset($_POST['bemerkungen']);
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', auftrag='$auftrag[$i]', bemerkungen='$bemerkungen[$i]' WHERE id='$id[$i]'";
$result1=mysqli_query($link, $sql1);
}
}
mysqli_close($link);
?> 
<input id="rfh" type="button" onclick="window.location.reload()" value="Refresh" />
</body>
	
</html>

Does someone know why. Google didn’t help me.

PS: I know the code above does not protect from sql-injection. It’ merely about a simple example file.

What happens? Precisely what do you mean by “can’t get it to work”?

I’d normally expect the processing code to be in before the form is drawn, or in a separate PHP file, but I’m not sure either is essential. This bit, though

$name = isset($_POST['name']);
$auftrag = isset($_POST['auftrag']);
$bemerkungen = isset($_POST['bemerkungen']);

will surely just return true or false for each? You seem to be using the result as arrays for your query.

2 Likes

Thank you, it was exactly the part of the code that prevented the form from functioning. I simply removed the isset().
Now I feel a bit stupid. :joy:

No need, you should check that form variables exist before you use them, so as to not get errors in your query. Just not like that :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.