7 Replies - 877 Views - Last Post: 02 September 2011 - 10:33 AM Rate Topic: -----

#1 thomasubc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 11

can not insert data into mysql server

Posted 02 September 2011 - 08:12 AM

The following code is for developing a database. But when i reach the point of inserting data into my table it does not function. What am I doing wrong?


<?php
$username="root" ;
$password="sbasyn" ;
$database= "partsx";
$link = mysql_connect('localhost:3306', $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
echo '<br>';
mysql_close($link);
$Description =$_POST["Description"];
$Part_Number =$_POST["Part_Number"];
$Document =$_POST["Document"];
print $Description ;
echo "<br>";
print $Part_Number;
echo "<br>";
print $Document;
echo "<br>";

print $username;
echo "<br>";
print $password;
echo "<br>";
print $database;
echo "<br>";
echo "This is before connecting to mysql";
mysql_connect('localhost:3306', $username, $password);
mysql_select_db($database);[u][i][b]
//(Up to this point I have verified working)
$create = "INSERT INTO partsx VALUES($Description,$Part_Number,$Document)";
mysql_query($create);

echo "<br>";
echo "This is after the connection has been established";
echo "<br>";
print $create;
mysql_close();
?>


This post has been edited by macosxnerd101: 02 September 2011 - 08:17 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: can not insert data into mysql server

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: can not insert data into mysql server

Posted 02 September 2011 - 08:17 AM

Add an or die(mysql_error()) clause after your mysql_query() function. See what error is displayed.
Was This Post Helpful? 1
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: can not insert data into mysql server

Posted 02 September 2011 - 08:22 AM

This is nothing to do with your issue, but there's really no need to use mysql_close();. It's implied once the script exits (same with file handles et al).
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: can not insert data into mysql server

Posted 02 September 2011 - 08:39 AM

Why are you connecting TWICE???

The (more) RIGHT way to do this, assuming part number is numeric:

if (!mysql_connect('localhost:3306', $username, $password)) {
    die('Failed to connect to database');
}
if (!mysql_select_db($database)) {
    die("Failed to select database $database");
}

$Description = mysql_real_escape_string($_POST["Description"]);
$Part_Number = $_POST["Part_Number"];
$Document = mysql_real_escape_string($_POST["Document"]);

$create = "INSERT INTO partsx VALUES('$Description',$Part_Number,'$Document')";
mysql_query($create) or die("Query $create failed: " . mysql_error());



Better would be to name your database columns specifically, assuming they match the variable names:

$create = "INSERT INTO partsx(`Description`, `Part_Number`, `Document`) VALUES('$Description',$Part_Number,'$Document')";

Was This Post Helpful? 1
  • +
  • -

#5 thomasubc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 11

Re: can not insert data into mysql server

Posted 02 September 2011 - 08:57 AM

View PostRudiVisser, on 02 September 2011 - 08:22 AM, said:

This is nothing to do with your issue, but there's really no need to use mysql_close();. It's implied once the script exits (same with file handles et al).



Thanks I have removed it and will no longer use it.
Was This Post Helpful? 0
  • +
  • -

#6 thomasubc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 11

Re: can not insert data into mysql server

Posted 02 September 2011 - 09:08 AM

Quote

Better would be to name your database columns specifically, assuming they match the variable names:

$create = "INSERT INTO partsx(`Description`, `Part_Number`, `Document`) VALUES('$Description',$Part_Number,'$Document')";


I agree with the last statement. So I changed it to be that.
My output is this...
Connected successfully
333
bbb
ccc
root
sbasyn
partsx
This is before connecting to mysqlFailed to select database partsx

The last part that says failed to select database partsx means that the
is failing? I have named the database partsx.
if (!mysql_select_db($database)) {
die("Failed to select database $database");
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: can not insert data into mysql server

Posted 02 September 2011 - 09:14 AM

My guess is permissioning, as you're connecting as a different user. Again, add mysql_error() for more info. PHP/MySQL debugging 101.

die("Failed to select database $database:" . mysql_error());

Was This Post Helpful? 0
  • +
  • -

#8 thomasubc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-September 11

Re: can not insert data into mysql server

Posted 02 September 2011 - 10:33 AM

View PostJackOfAllTrades, on 02 September 2011 - 09:14 AM, said:

My guess is permissioning, as you're connecting as a different user. Again, add mysql_error() for more info. PHP/MySQL debugging 101.

die("Failed to select database $database:" . mysql_error());



It was so simple it hurts to share.
$database= "parts";
The database was named parts and the table was named partsx. I had both named as as partsx. Thank you very much for all your help. I will most likely have to post more as this is only my first week of PHP and MySQL. I appriciate all the help

Thank you,
Very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1