I have a form that provides the user with an option to post within 1 of 3 columns. If the user decides they want to post in the first column, then they click the column 1 button and my jquery script removes the other 2 input fields.
The issue is that each one of these columns are linked to its own table within the database, so every time a user enters information into 1 of the columns, it's sent to the landing page where it updates all 3 tables.
If the user where to select Column 1, then their information will be inserted in to the Column 1 table, but blank rows will also be inserted into table 2 and 3.
<?php
$con=mysqli_connect("URL", "DB", "password","DB_Name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$Main= mysqli_real_escape_string($con, $_POST['Column_1']);
$storyn1 = mysqli_real_escape_string($con, $_POST['storyn1']);
$sql="INSERT INTO Column_1 (Link1, storyn1)
VALUES ('$Main', '$storyn1')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
$con=mysqli_connect("URL", "DB", "password","DB_Name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$MLB= mysqli_real_escape_string($con, $_POST['MLB']);
$storyn2 = mysqli_real_escape_string($con, $_POST['storyn2']);
$sql="INSERT INTO Column_2 (Link, storyn2)
VALUES ('$MLB', '$storyn2')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
$con=mysqli_connect("URL", "DB", "password","DB_Name");;
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$Column_3= mysqli_real_escape_string($con, $_POST['Column_3']);
$storyn3 = mysqli_real_escape_string($con, $_POST['storyn3']);
$sql="INSERT INTO Column_3 (Link3, storyn3)
VALUES ('$Column_3', '$storyn3')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
I feel like the reason I'm getting blank rows inserted into my table is because this page contains insert code for all 3 tables. Whats the best practice for this? Do I need to have 3 separate landing pages to house these insertion codes?
Here is the updated Code from the suggestions below:
// escape variables for security
$Main= mysqli_real_escape_string($con, $_POST['Column_1']);
$storyn1 = mysqli_real_escape_string($con, $_POST['storyn1']);
$MLB= mysqli_real_escape_string($con, $_POST['MLB']);
$storyn2 = mysqli_real_escape_string($con, $_POST['storyn2']);
$Column_3= mysqli_real_escape_string($con, $_POST['Column_3']);
$storyn3 = mysqli_real_escape_string($con, $_POST['storyn3']);
if(!empty($_POST['Column_1']) && !empty($_POST['storyn1'])) {
$sql="INSERT INTO Column_1 (Link1, storyn1)
VALUES ('$Main', '$storyn1')";
}
if(!empty($_POST['MLB']) && !empty($_POST['storyn2'])) {
$sql="INSERT INTO Column_2 (Link, storyn2)
VALUES ('$MLB', '$storyn2')";
}
if(!empty($_POST['Link3']) && !empty($_POST['storyn3'])) {
$sql="INSERT INTO Column_3 (Link3, storyn3)
VALUES ('$Column_3', '$storyn3')";
}
?>