Blank row every refresh

Codes here !

Moderators: egami, macek, gesf

Post Reply
Blobbob178
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jan 23, 2018 9:44 am

Tue Jan 23, 2018 10:11 am

So, I have this code:

Code: Select all

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 want it only if I click on the button.

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

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

Code: Select all

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

Code: Select all

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.

The whole code:

Code: Select all

<?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);
    
?>
Thanks in advance.


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

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 54
Joined: Mon Oct 16, 2017 1:06 pm

Tue Jan 23, 2018 3:46 pm

Add' is a input/submit button which should create blank rows for my sql table
Why in the world do want to insert blank rows in the database?
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Tue Jan 23, 2018 10:30 pm

Hello, blobbob178.

Your question is a little unclear.
Try to demonstrate with images or explain further more details would be lovely.
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

Blobbob178
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jan 23, 2018 9:44 am

Tue Jan 23, 2018 10:38 pm

As explained above, every time I refresh a new blank row gets added although it should only on button click. I would like to rewrite the code, so that a refresh doesn't produce that behaviour for the code itself without the second $_POST value in the if statement would work. It does add new row on button click. However it does so on refresh, too.
A member on stackoverflow told me to use the second $_POST value in the if statement (referring to my initial question). However I didnt't understand what the value should be.
lf there's another way to suppress the refresh from adding new blank rows, I would be glad to hear it!

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Wed Jan 24, 2018 4:14 am

Hello, Blobbob178.

Apparently, your code should have worked even without the $_POST value (which the member from the other forum insisted you should create another check).
The code on the query looks fine (well, to me) and the logic looks fine too.

I would suggest you to remove the second check for the time being. I am sorry that I could not proceed further to assist you but I will try to reproduce your matter. It would be easier for me to check if you can export your database.
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 54
Joined: Mon Oct 16, 2017 1:06 pm

Wed Jan 24, 2018 10:21 am

There is no reason you should be inserting blank rows.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Post Reply