SomeAnonymousPerson SomeAnonymousPerson - 3 months ago 7
MySQL Question

Not enter data in mysql database with wordpress and php

Im trying to add a new user name to mysql table throw wordpress. But everytime I try to do it, I have no error message, but there are no lines added to the data base.

This is the wordpress page with the php inside:

<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td> </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("foundint_Sababa", $con);

$sql="INSERT INTO `Users` (`sName`, `sEmail`)
VALUES ('{$_POST['sName']}','{$_POST['sEmail']}')";


if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

$info = mysql_info(); echo $info;

mysql_close($con);
?>


I can't see whats wrong. I think it may be something wrong with the connection. Any ideas?

Thank you!

Edit

As suggested, Im trying to use
$wpdb
so I've created a php file in a folder call my-codes (at the same level of wp-admin, wp-content and wp-includes) and I added the following code to a file call insertUser.php:

<?php
global $wpdb;
$wpdb->insert("wp_submitted_form", array(
"sName" => $sName,
"sEmail" => $sEmail));
?>


Now in my page Im trying to call this function and Im doing this:

<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td> </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>
<?php
if(isset($_POST['Submit']))
{
include("./my-codes/insertUsers.php");
}
?>


And im still not being able to insert any row in the database. Any suggestions?

EDIT
I needed a pluggin to actually connect my sql database with wordpress. The code is correct.

Answer

Replace your code with the code that i have provided.

    <table>
    <form name="form1" method="post" action="">
    <strong>Please enter your information in order to download the Macs Cabs     
    App</strong>
    <tr><td>
    Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
    Email Address:</td><td><input name="Email" type="text" id="sEmail"></td>   </tr>
    <tr><td>
    <input type="submit" name="Submit" value="Submit"></td></tr>
    </form></table>

    <?php
    $con = mysql_connect("localhost","root","root"); // ensure that your password in empty or root in your localhost
    if (!$con)
    {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("foundint_Sababa", $con);

    if(isset($_POST['Submit']))
    {
       // Previous Insert Query which has discrepency in form input names and Insert Values 
      //$sql="INSERT INTO `Users` (`sName`, `sEmail`) VALUES ('{$_POST['sName']}','{$_POST['sEmail']}')";

      // My new Query with corrected form input names for Input Values during POST.
        $sql = "INSERT INTO `Users`(`sName`, `sEmail`) VALUES ('".$_POST['Name']."','".$_POST['Email']."')";
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
        echo "1 record added";
        $info = mysql_info(); echo $info;
        mysql_close($con);
    }

Finally have a check at the table field Names and my code works fine hope it will serve you to.