We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,672 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Adding rows to an external SQL table

Hey there. I'm having a bit on an issue with adding new data to an SQL table on my webserver. I'm trying to update data in the table through php, but for an unknown reason the script returns no errors but refuses to update the SQL table with the content I provide.

<?php
  $databasename = $_POST["databasename"];
  $databaseuser = $_POST["databaseuser"];
  $databasepass = $_POST["databasepass"];

  $connection = mysql_connect("localhost", $databaseuser, $databasepass);

  if (!$connection) {
    echo "A connection could not be made to the database! Possible issues: Your username and/or password may have been incorrect!";
    return;
  }

  $seldb = mysql_select_db($databasename, $connection);

  if (!$seldb) {
    echo "An error occured while selecting the database!";
    return;
  }

  $tablename = $_POST["tablename"];

  if (!tableExists($tablename)) {
    echo "The table name you entered doesn't exist!";
    return;
  }

  $tablecol = $_POST["colname"];
  $tablecoldat = $_POST["coldata"];

  if (!$tablecol) {
    echo "You must enter a collumn name to enter data into!";
    return;
  }
  $req =
  "INSERT INTO " . $tablename . " (
    '" . $tablecol . "'
  )
  VALUES (
    '" . $tablecoldat . "'
  )";
  mysql_query($req, $connection);
  mysql_close();
  echo "Script end!";

  /*
    FUNCTIONS
  */
  function tableExists($tbl) {
    global $connection;
    $dat = mysql_query("SELECT * FROM " . $tbl, $connection);
    if (!$dat) {
      return false;
    } else {
      return true;
    }
  }
?>

I'm pretty new to PHP, and I've only been studying it for a couple of days so I apologize if my code seems messy, inefficient or just plain garbage. Help would be appreciated if you can give it though. If you need any more information on what I'm doing just ask. As far as I know this is all I need to post.

The database name: aaron_test_database
The table name: aaron_test_table

2
Contributors
2
Replies
16 Hours
Discussion Span
3 Months Ago
Last Updated
7
Views
Question
Answered
Aaron_JY
Newbie Poster
9 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try to change quotes with backticks in the field names of the INSERT statement, as here:

$req = "INSERT INTO $tablename(`$tablecol`) VALUES ('$tablecoldat')";

Also, to catch an error use mysql_error() at least in this stage:

 mysql_query($req, $connection) or die(mysql_error());
cereal
Veteran Poster
1,144 posts since Aug 2007
Reputation Points: 344
Solved Threads: 221
Skill Endorsements: 22

That's fantastic. Thanks for your help! I edited my code to match the first sample you posted, and it worked fantasically.

Aaron_JY
Newbie Poster
9 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by cereal

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0603 seconds using 2.67MB