Results 1 to 7 of 7

Thread: Newbie - Insert data into table.

  1. #1
    Join Date
    Jul 2013

    Newbie - Insert data into table.

    I am using PHP 5.4.9 and MYSQL 5.7. I have a php file created that shows I am connecting to the database, yet I am unable to insert data into the table using a PHP file. Could someone point me to a good sample script?

    //set up static variables
    $page_name = "WEB Index"
    $Page_desc = "This is a sample."
    $date_added = date("mm-dd-yy");

    //set up table and database names
    $db_name = "countdb";
    $table_name = "hit_table";

    //connect to server and select database
    $connection = mysql_connect('localhost', 'root', '0304Phreq');

    $db=mysql_select_db ($db_name, $connection);

    //build and issue query
    $sql = "insert into $table_name values(",'$page_name', '$Page_desc', '$date_added')";
    $result = mysql_query ($sql, $connection);

    //get count
    $count_sql = "select count(page_name) from $table_name
    where page_name = '$page_name'";
    $count_res = mysql_query($count_sql, $connection);
    $count = mysql_result($count_res, 0 , "count(page_name)");



    <TITLE>Sample Page </TITLE>
    <h1>Useless Sample</h1>
    <P>This serves no purpose</p>
    <P>Accesses: <? echo "$count"; ?></p>

    Any suggestions?

  2. #2
    Join Date
    Aug 2004
    First thing I'd do is add some defensive coding to catch any errors, and log/output info to find out what went wrong. For example, where you execute the query:
    PHP Code:
    $result mysql_query ($sql$connection);
    $result == false) {
    error_log("Query failed:".PHP_EOL.mysql_error().PHP_EOL.$sql); // or output to browser for now if you want
    die("Query failed, see error log");

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Sep 2011
    I think your problem there
    $sql = "insert into $table_name values(",'$page_name', '$Page_desc', '$date_added')";
    you building sql statement like
    insert into myTable values(",'login.php', 'secure login page', '12/5/2013')
    but all you $table_name, $page_nameect it is variables in php, so when you send line
    insert into $table_name values(",'$page_name', '$Page_desc', '$date_added')
    server do not know $table_name...
    so your first sql should be
    $sql = "insert into " + $table_name + " values(",'" + $page_name + "', '" + $Page_desc + "', '" + $date_added + "')";
    and second
    $count_sql = "select count(page_name) from " + $table_name + " where page_name = '" + $page_name + "'";

  4. #4
    Join Date
    Jul 2013
    Thanks. The error log does not show anything.

  5. #5
    Join Date
    Jul 2013
    Thanks. The additional coding did not help.

  6. #6
    Join Date
    Oct 2012
    What do you want? To insert data into the SQL through running a file one time. E.g. you make a list of items and you run the file once and it inserts every record into the database and then your done. Or a HTML page where a said user inserts text into textboxes clicks submit and they are in putted into the database?

  7. #7
    Join Date
    Jul 2013
    I am trying to create a hit counter that stores information in a database. i.e. page name date of hit, etc. Every connection to the page would be accounted in the database.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center