I have been trying to come up with some way to get a html form i made on a wordpress page to take the information entered and send it to custom tables in the wordpress database. I have tried calling a custom php script and writing code in the page itself and i cannot not figure out how to get it to work. I am going crazy trying to figure this out, I am about to give up. Any suggestions on where to start looking, enough i have been searching for a week, would be great.
I have tried using mysqli_connection but i keep getting issue connecting where i put (mysite.com,user,pass,db); and i get another error “cannot connect “user@anotherunknownsite.com” i have a shared ip so i wonder if it is connecting to this other site, which has NO reference in my code. I tired using local host instead of my site but nothing works.
This code tries to get some data off the db but nothing happens, same as b4 where mysite.com shows a error cant connect to user@randomsite.com and localhost doesnt work.
<?php
$con=mysqli_connect("mysite.com","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Liquor_Type");
while($row = mysqli_fetch_array($result)) {
echo $row['lq_name'] . " " . $row['description'];
echo "<br>";
}
mysqli_close($con);
?>
my form
<form action = "setLQType.php" method="post">
Add a New Liquor Type</br></br>
<p>Name: <input type="text" name="name"/></p>
<p>Description <input type="text" name="description"/></p>
----------------------------------------------
<input type="submit" name="Submit"/>
</form>
</br>
The setLQType php file, which is in the main wordpress dictionary. It will got to the file but do nothing.
<?php
$con=mysqli_connect("mysite.com","user","pass","db_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = mysqli_real_escape_string($con, $_POST['name']);
$description = mysqli_real_escape_string($con, $_POST['description']);
$sql="INSERT INTO Liquor_Type(lq_name, description)
VALUES ('$name ', '$description)";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Updated insert page… setLiquorType.php in website.com/setLiquorType.php
<?php
$global $wpdb;
$name = $_POST['name'];
$description = $_POST['description'];
$table_name = $wpdb->prefix . "wp_Liquor_Type";
$wpdb->insert($table_name, array(
'lq_name' => $name,
'description' => $description
));
?>
Posting works like this:
Your mysql connection is available in the var $wpdb
I recommend to use WP functions to do the work. You need the global object
$wpdb
to use them. For insert try with$wpdb->insert($table, $array)
.You can define a shortcode that you put in
functions.php
or make a little plugin for this. In the same function yo can paint the form and try to get the data from it (thought I prefer to get the data first and paint the form the last)Here you have a code that works: