Note that there are some explanatory texts on larger screens.

plurals
  1. POForm submitting/inserting data to mySQL database on page load
    primarykey
    data
    text
    <p>I am having an issue with one of my .php pages.</p> <p>adddata.php has a form which allows a user to submit certain data to the database however each time the page loads or is refreshed, the empty form is submitted (creating a row of blank values in the DB).</p> <p>I have little experience with PHP so apologies in advance for any novice mistakes - here is the code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Project IPAM - Add Data&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container" class="container" style="width:980px"&gt; &lt;?php include('./templates/header.php'); include('./templates/dbconnect.php'); ?&gt; &lt;?php if(isset($_POST['submit'])) { $name = $_POST['address_v4']; echo "Successful submitted"; } ?&gt; &lt;div id="content" style="background-color:#ABCDEF;height:500px;width:980px"&gt; &lt;!-- This is a example submit form to add data to the database !--&gt; &lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1"&gt; &lt;tr&gt; &lt;td&gt;&lt;form method="post" action="adddata.php"&gt; &lt;table width="" border="0" cellspacing="1" cellpadding="3"&gt; &lt;tr&gt; &lt;td colspan="3"&gt;&lt;strong&gt;Insert Data Into mySQL Database &lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;IP(v4)&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="address_v4" type="text" id="address_v4" maxlength="15"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Hostname&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="hostname" type="text" id="hostname" maxlength="32"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Type&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;select name="type"&gt; &lt;option value="static"&gt;Static&lt;/option&gt; &lt;option value="dhcp"&gt;DHCP&lt;/option&gt; &lt;option value="reserved"&gt;Reserved&lt;/option&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Updated By&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="updated_by" type="text" id="updated_by" maxlength="16"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Notes&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="notes" type="text" id="notes" maxlength="128"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Location&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;textarea cols="17" rows="7" name="location" type="text" id="location" maxlength="64"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Default Gateway&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="default_gateway" type="text" id="default_gateway" maxlength="15"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Subnet Mask&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="subnet_mask" type="text" id="subnet_mask" maxlength="15"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3" align="center"&gt;&lt;input type="submit" name="submit" value="Submit"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;?php // Connect to the DB include_once('./templates/dbconnect.php'); // Get values from form and checks whether values have been initilized using the isset() function. $address=isset($_POST['address_v4']); $hostname=isset($_POST['hostname']); $type=isset($_POST['type']); $updated_by=isset($_POST['updated_by']); $notes=isset($_POST['notes']); $location=isset($_POST['location']); $default_gateway=isset($_POST['default_gateway']); $subnet_mask=isset($_POST['subnet_mask']); // Insert data into mysql $sql="INSERT INTO $tbl_name(address_v4, hostname, type, updated_by, notes, location, default_gateway, subnet_mask) VALUES('$address','$hostname','$type','$updated_by','$notes','$location','$default_gateway','$subnet_mask')"; if (!mysql_query($sql,$con))// { die('Error: ' . mysql_error()); } mysql_close($con) ?&gt; &lt;/div&gt; &lt;div id="footer" style="background-color:#FFA500;clear:both;text-align:center;"&gt; Copyright © 2011 Richard Day &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload