Kristopher Therrien
Kristopher Therrien

Reputation: 89

Mysql insert not working and not giving errors

i do not know why the following code will not work for inserting data into mysql.

if (!$link = mysql_connect('server', 'user', 'password')) {
    echo '700';
    exit;
}

if (!mysql_select_db('vendors', $link)) {
    echo '701';
    exit;
}
$sql2 = "INSERT INTO transactions (TransID, payment_status, last_name, first_name, payer_email, address_name, address_state, address_zip, address_country, verify_sign, payment_gross, ipn_track_id, business, reciver_email) VALUES ('kris', 'kris', 'kris', 'kris', 'kris','kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris', 'kris')";

$result2 = mysql_query($sql2, $link);

What is wrong with the code? php is giving no errors.

Upvotes: 0

Views: 111

Answers (3)

Hirdesh Vishwdewa
Hirdesh Vishwdewa

Reputation: 2362

Please try not to use mysql_connect instead use mysqli_connect or PDO_MySQL read this

Also use die to find if there is any errors in your code

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

Otherwise(recommended way)-

Procedural style

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', '[email protected]')";

if (mysqli_query($conn, $sql)) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

MySQLi Object-oriented style

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', '[email protected]')";

if ($conn->query($sql) === TRUE) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

Upvotes: 2

varad mayee
varad mayee

Reputation: 619

You have to write the code like below to get the errors in your code

$result = mysql_query($sql2,$link) or die(mysql_error());

this or die(mysql_error()) will give you errors in query

Upvotes: 0

Pa3k.m
Pa3k.m

Reputation: 1166

Try changing this

 $result2 = mysql_query($sql2, $link);

Into this

$result2 = mysql_query($sql2, $link)or die(mysql_error());

Upvotes: 0

Related Questions