SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    PHP not working. Insertion into SQL database

    Here is all my code.
    checkInfo.php

    Code:
    <?php
    
    include ("config.php");
    ?>
    <!doctype html>
    <html>
    <head>
    <title>check</title>
    </head>
    <body>asdfasdf
    <?php
    
    $contentOfPost=$_POST['contentOfPost'];
    $submit=$_POST['submit'];
    $date = date("Y/m/d");
    $time = time();
    
    echo $date;
    echo $time;
    echo $submit;
    echo "<br />";
    echo $contentOfPost;
    
    $link = mysql_connect($host, $username, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    else{
    echo 'Connected successfully';
    
    $insertionToDatabase=INSERT INTO userSubmitted ('PostNumber', 'Date', 'Time', 'Content') VALUES (NULL, '$date', '$time', '$contentOfPost');
    
    mysql_close($link);
    }
    
    ?>
    
    </body>
    </html>

    config.php
    Code:
    <?php
    $host="mysql";
    $username="thisisnthere";
    $password="thisisfalse";
    ?>


    blog.php
    Code:
    <?php
    session_start();
    
    ?>
    <!doctype html>
    <html>
    <head>
    <title>blog</title>
    <style type="text/css">
    
    </style>
    </head>
    <body>
    <?php
    session_start();  
    $_SESSION['views'] = 1; // store session data
    if(isset($_SESSION['views']))
        $_SESSION['views'] = $_SESSION['views']+ 1;
    else
        $_SESSION['views'] = 1;
    
    echo "views = ". $_SESSION['views']; 
    ?>
    <form action="checkInfo.php" method="post">
    <textarea name="contentOfPost">
    
    </textarea>
    <input type="submit" value="Submit Blog" name="submit" />
    </form>
    </body>
    </html>


    Basically this was working except when I tried to write the user submitted post into the database. No errors appear or anything, just a blank page. I've written some if/else statements to try and just get ANYTHING to appear but it's a no go. What am I missing?

    I know that the sql connection is correct because I've tested it before.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Crazy colored text, can hardly read that....

    You are using PHP to build an SQL string. Did PHP do what you expected?

    add this line:
    PHP Code:
    echo $insertionToDatabase
    Look at the output very carefully, paste it into your database via your db management tool. Was it a valid SQL statement or not?

    If no, then work backwards, echo out the PHP variables all the way back to the form if necessary - and check the syntax of your sql statement again.

    If it was a valid SQL statement, then work forwards -- why did it not reach your database? Double check your connections again, just because they used to work does not mean they still do. Hardcode the details into the script temporarily to identify what is going wrong where.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by RyanReese View Post
    Code:
    $insertionToDatabase=INSERT INTO userSubmitted ('PostNumber', 'Date', 'Time', 'Content') VALUES (NULL, '$date', '$time', '$contentOfPost')
    and yet still ~another~ developer kneecapped by the horrible mysql backticks

    ryan, your list of column names that follows the table name is actually a list of strings

    get rid of those quotes -- they shoulda/coulda been backticks, but it's best never to use backticks at all, ever
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Hi, I tried b oth suggestions.

    The issue is NOTHIGN is appearing. No text at all. Even if I hardcode HTML into the page.

    I tried both suggestions but still nothign is appearing.

    I go to blog.php, type in some random nonsense and hit submit. Upon it coming to checkInfo.php, it's a blank page. Even no HTML shows up in the source.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i don't do php, but i think your $insertionToDatabase needs some kind of quotes around the entire sql string, and also, don't you need to run it into the mysql_query function?

    anyhow, test the query outside of php first, to make sure it's working
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I don't think my SQL insertion is working. I narrowed it down to my SQL insertion variable as being the cause for stuff not showing up.

    Even if I go to INSERT in PHPmyadmin to give me a basic outline of the insertion SQL query, even when I copy that into my PHP file, it doesn't work.

    Basically I have four rows in my table userSubmittedData (in table blog) and the first row is auto incremented, second row is date, then time, then the actual content of the blog post.

    INSERT INTO `blog`.`userSubmitted` (`PostNumber`, `Date`, `Time`, `Content`) VALUES (NULL, '2012-02-15', CURRENT_TIMESTAMP, 'thisisapost');

    That's an example SQL query I got when I manually inserted something. It came with the ticks.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  7. #7
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I think it is working now. The mysql_query was the issue, and the quotes.

    I'll give updates as I go along...
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  8. #8
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I'm now trying to get a loop going so it displays all of the posts I submit.

    Code:
    <?php
    session_start();
    include "config.php";
    $i=0;
    $query="SELECT PostNumber
    FROM userSubmitted
    ORDER BY PostNumber ASC";
    
    $result=mysql_query($query);
    if ($!result)
    {echo "you suck";}
    else{echo "yay";}
    
    $num=mysql_numrows($result);
    
    
    
    echo "<b><center>Database Output</center></b><br><br>";
    
    
    while ($i < $num) {
    
    $field1-name=mysql_result($result,$i,"PostNumber");
    $field2-name=mysql_result($result,$i,"Date");
    $field3-name=mysql_result($result,$i,"Time");
    $field4-name=mysql_result($result,$i,"Content");
    
    echo "<b>$field1-name
    $field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";
    
    $i++;
    }
    mysql_close();
    ?>
    Where am I wrong? Besides the noob HTML I put in there. I just was getting throwing it in there.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    you can't display 4 different columns if you only retrieve one column with your query
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    This is my updated code. For reference
    Code:
    asdf<?php
    session_start();
    include "config.php";
    $link = mysql_connect($host, $username, $password);
    $i=0;
    $query="SELECT * FROM userSubmitted";
    
    
    $result=mysql_query($query);
    if (!$result)
    {die("this didn't work");}
    else{ echo "yay";
    }
    
    
    echo $result;
    $num=mysql_num_rows($result);
    echo $num;
    
    //while ($i < $num) {
    //
    //$field1-name=mysql_result($result,$i,"PostNumber");
    //$field2-name=mysql_result($result,$i,"Date");
    //$field3-name=mysql_result($result,$i,"Time");
    //$field4-name=mysql_result($result,$i,"Content");
    //
    //echo "<b>$field1-name
    //$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";
    //
    //$i++;
    //}
    mysql_close();
    ?>
    Output is "asdfthis didn't work"
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  11. #11
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,624
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I fixed it all. Thanks.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com


Bookmarks

Posting Permissions

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