Why is there a blank row on every refresh?

So, I have this code:

if (isset($_POST['Add']) and $_POST['What to insert']!="") {
        $sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
        $result2=mysqli_query($link, $sql2);
    }

What do I have to insert where I wrote ‘what to insert’ ?
‘Add’ is a input/submit button which should create blank rows for my sql table. However it does so every time I refresh the page which is not what I want.

I asked this question earlier in the stackoverflow forums but no one is answering me any longer. Does anybody have an idea? Do you need the whole code to answer? A member in the stackoverflow forum said:

Try this and in You Post Value pass your post array index.

if (isset($_POST['Add']) and $_POST['You_post_value']!="") {
    $sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
    $result2=mysqli_query($link, $sql2);
}

Or

if (isset($_POST['You_post_value']) and $_POST['You_post_value']!="") {
    $sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
    $result2=mysqli_query($link, $sql2);
}

Note: And it should be your post value, Ignore button click.

This is what he answered. But I didn’t understand what he meant.

Thanks in advance.

You’ll need to expand on that. Where is the rest of the code? No one is going to be able to answer your question on the basis of this tiny snippet of code.

You’re not providing any actual data in the query. DEFAULT will set the column to the default for that column, which is probably an empty string. You need to replace DEFAULT in the query with the data that you actually want inserted.

Sorry about that. The whole code:

 <?php
        
    ini_set('display_errors', '1');


    $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);
?>



<html>    
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700&amp;subset=latin-ext" rel="stylesheet" media="screen">
<title>IT Battenberg</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" media="screen">
<link rel="stylesheet" type="text/css" href="horst.css.php" media="screen">
<link rel="stylesheet" type="text/css" href="file.css.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="file://///nas-it/">NAS</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>
	
	

<div id="large-container" class="large-container">
    


    <table id="tbl">
    <form name="form1" method="post">
    <tr class="row">
    <td>
    <table id="tbl1">

    <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">
        <textarea class="ipt" name="name[]" type="text" id="name"><?php echo $rows['name']; ?></textarea>
    </td>
    <td align="center">
        <textarea class="ipt" name="auftrag[]" type="text" id="auftrag"><?php echo $rows['auftrag']; ?></textarea>
    </td>
    <td align="center">
        <textarea class="ipt" name="bemerkungen[]" type="text" id="bemerkungen"><?php echo $rows['bemerkungen']; ?></textarea>
    </td>
    </tr>

    <?php
    }
    ?>
	
	<tr class="row">
    <td align="center"><input style="background: #47a7a3; border: none; font-size: 1em; color: yellow;" id="rfh" type="button" onclick="window.location.reload()" value="Refresh" /></td>
    <td colspan="4" align="center"><input style="background: #47a7a3; border: none; font-size: 1em; color: yellow;" id="sbt" type="submit" name="Submit"></td>
    <td align="center"><input id="add" type="submit" name="Add" value="Add Row"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
	</table>
	
</div> 


</body>
    
</html>



<?php


    // Count table rows
    $count = mysqli_num_rows($result);
    

    $name = $_POST['name'];
    $auftrag = $_POST['auftrag'];
    $bemerkungen = $_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);
    }
    }
    
    
    if (isset($_POST['Add']) and $_POST['what to insert?']!="") {
        $sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
        $result2=mysqli_query($link, $sql2);
    }


    mysqli_close($link);
    
?>

PS: I know it does not protect from sql injection and has other flaws but that is not yet important to me. I’m just trying to learn and get a few working examples.

The way I would do it would be to have a separate form, containing whatever fields are required, and submit that, rather than having a second submit button within the same form as you have done. At the moment it seems that you just have an “Add Row” button - if adding a blank row and then re-displaying the table is the way you want to do it, then that should work without that second $_POST value comparison. But I think I’d still put that into a separate form.

Thank you for the answer. I will keep that in mind and try to move the stuff to a separate form.

The thing is if I do it without the second $_POST value it adds a new row every time I refresh the browser which I don’t want.

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