Open Side Menu Go to the Top

06-03-2011 , 06:48 AM
Ok, in answer to your original question you can append the port to the end of the domain like follows:

Code:
mysql_connect("localhost:3307","panelUserId_DBuser","password");
mysql_select_db("UserId_dataBase");
You probably don't need to specify the port (I think the default is 3007) so if you set it up with default settings you are fine.

Interesting side note you can also specify the port with http requests, eg:

http://www.scirra.com:80

But there's really very little application for it
HTML5/CSS3/advancedJavascript learning and programming logs Quote
HTML5/CSS3/advancedJavascript learning and programming logs
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
HTML5/CSS3/advancedJavascript learning and programming logs
06-03-2011 , 01:50 PM
Most MySQL DBs on shared hosts or VPSes are configured to only listen in on the local loopback unless explicitly told otherwise. If this is not the case, try to set it up this way.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 12:26 AM
So, the connection is confirmed. This page seems to be completely working, but still....

When I press "submit," nothing shows up in the database. What's up with that.

I moved everything over to one page.

Code:
<?php

require('connect.php');
$name = $_POST['name'];
$comment = $_POST['commentary'];
$submit = $_POST['submit'];

if($submt){
    if($name&&$comment){
        $insert=mysql_query("INSERT INTO comments (name, comment) VALUES ('" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($comment) . "')"); 
    }else{
        echo "Please fill out all fields";
    }
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />



</head>

<body>

<form action="comment.php"
    method="POST">
    <fieldset>
        <legend>Comments:</legend>
        <p>While I am a strong proponent of freedom of speech and expression, I am granted with the right to delete spam, troll posts, and rudeness. I will exercise that right when needed.</p>
        
        <p><label>Name:<br><input type="text" name="name"></label></p>
        <p><label>Comments:<br><textarea name="commentary"></textarea></label></p>
        
        <p><button type="submit" name="submit" id="submit">Hit me</button></p>
        
    </fieldset>
</form>

<?php

$getquery=mysql_query('SELECT * FROM comments ORDER BY id DESC');

while($rows=mysql_fetch_assoc($getquery)){
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];

    echo $name . '<br>' . $comment . '<br>' . '<br>';
}
?>

</body>
</html>
I really should have used a free comment box builder, but no: I have to do everything the hard way.

Yeah, I'll change the SELECT * FROM to the way Gullanian suggested as well. That seems to make sense since if I want to have a comment box in each page, it would probably be easier to fetch the comment by domain/subject as opposed to creating a new database for each page.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 12:42 AM
Code:
$submit = $_POST['submit'];

if($submt){...
You forgot the i in submit. You should probably use isset() though to see if the submit button has been pressed. Been a while since I worked with PHP though.

It would also be a reasonable idea to move the escape string functions out of the SQL command itself and do that on top when you set the variable. It'll be easier to manage later.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 02:27 AM
Found a handy bit of code: error_reporting(E_ALL);

the output is as follows:

Notice: Undefined index: name in xxxxx on line 6

Notice: Undefined index: commentary in xxxxx on line 7

Notice: Undefined index: submit in xxxxx on line 8

which is telling me there is an error on these lines:

Code:
$name = $_POST['name'];
$comment = $_POST['commentary'];
$submit = $_POST['submit'];
Hooray error handling!
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 02:29 AM
This will be the most useful function in PHP you will ever have.

print_r();

Alternatively, var_dump();
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 02:39 AM
Maybe this is getting closer now:

The new error is:
Undefined variable: submit in xxxxxx on line 18

After looking up the index issue, see the isset() stuff, and plugged it in:

Code:
<?php

error_reporting(E_ALL);

require('connect.php');
if (isset($_POST['name'])){
    $name = $_POST['name'];
}

if (isset($_POST['commentary'])){
    $comment = $_POST['commentary'];
}

if (isset($_POST['submit'])){
    $submit = $_POST['submit'];
}
And line 18 starts here:

Code:
if($submit){
    if($name&&$comment){
        $insert=mysql_query("INSERT INTO comments (name, comment) VALUES ('" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($comment) . "')"); 
    }else{
        echo "Please fill out all fields";
    }
}

?>
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 02:52 AM
The new errors are as follows:

Notice: Undefined index: submit in xxxxxxx on line 14

Notice: Undefined variable: submit in xxxxxxxx on line 18
^^^^ location of print_r($submit);

Notice: Undefined variable: submit in xxxxxxxxxxx on line 20

Should "submit" really be called a variable here? Shouldn't it be more like a function initializer?
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 03:09 AM
No, it's a variable. You are checking to see if $submit is set to something not FALSE by doing that.

$submit is not set because it is not in the POST array. print_r() $_POST.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 03:36 AM
To save you hours of testing replace the button with:
Code:
<input type="submit" name="submit" id="submit" value="Hit me" />
If you want it to work with the button element instead of the above then you can research that on your own. At least now you know why it's not working.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 03:39 AM
Ah, name and id. Belt and suspenders. I wish I could say it's not needed...
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 04:02 AM
I associate name with server and id with client when it comes to form processing. The server needs the name to actually pick it up and having the id is nice on the client side so you can reference it with getElementById().

I don't know if this changed much recently. My brain just defaults to using both when dealing with forms.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-04-2011 , 05:47 AM
I guess I'll never use a button element ever again. Probably why that javaScript broke too.

So stupid: I was nagging myself about changing the button to an input type and just never did it. Sometimes I have to listen to my mostly misguided instincts.

Tomorrow (and probably the next 3 weeks):

- attempt to place all the php into an external php file and keep the form on an html file.

- figure out how to auto-query the php to insert/get via subject or url so that I can have two html files with functional and not confused forms.

- press submit and have the php send an email to me. Later, attempt to make the email state where the comment was placed.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 12:33 AM
Continuing on with the dreaded comment boxes....

I created a second file, called it commentTwo.php and the first one is called comment.php.

The issue with the comment box is a so-called "reload error." The fix is pretty easy, though obvious.

There's going to be redundant copy/paste here, so bear with me.

comment.php

Code:
<?php
require('connect.php');
if (isset($_POST['name'])){
    $name = $_POST['name'];
}

if (isset($_POST['commentary'])){
    $comment = $_POST['commentary'];
}

if (isset($_POST['submit'])){
    $submit = $_POST['submit'];
}

if($submit){
    if($name&&$comment){
        $insert=mysql_query("INSERT INTO comments (name, comment) VALUES ('" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($comment) . "')"); 
    }else{
        echo "Please fill out all fields";
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />



</head>

<body>

<form action="comment.php"
    method="POST">
    <fieldset>
        <legend>Comments:</legend>
        <p>While I am a strong proponent of freedom of speech and expression, I am granted with the right to delete spam, troll posts, and rudeness. I will exercise that right when needed.</p>
        
        <p><label>Name:<br><input type="text" name="name"></label></p>
        <p><label>Comments:<br><textarea name="commentary"></textarea></label></p>
        
        <input type="submit" name="submit" id="submit" value="Hit me" />
        
    </fieldset>
</form>


<?php

$getquery=mysql_query('SELECT * FROM comments ORDER BY id DESC');

while($rows=mysql_fetch_assoc($getquery)){
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];

    echo $name . '<br>' . $comment . '<br>' . '<br>';
}

?>

</body>
</html>
In order to kill the redundancy error, just change the following (only the heading php stuff):

Code:
<?php
require('connect.php');
if (isset($_POST['name'])){
    $name = $_POST['name'];
}

if (isset($_POST['commentary'])){
    $comment = $_POST['commentary'];
}

if (isset($_POST['submit'])){
    $submit = $_POST['submit'];
}

if($submit){
    if($name&&$comment){
        $insert=mysql_query("INSERT INTO comments (name, comment) VALUES ('" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($comment) . "')");
        header('Location: direct.php'); 
    }else{
        echo "Please fill out all fields";
    }
}

?>
That header function-y thing connects to the direct.php:

Code:
<?php
header('Location: comment.php');
?>
Which is basically looping stuff back and forth for the fun of doing it apparently. j/k

What I found fascinating is that if I don't put the header function into commentTwo.php, it posts the comment into comment.php and commentTwo.php, but if I place the comment into commentTwo.php, I am taken to comment.php.

So, adding the header to commentTwo.php doesn't change this behavior.

It seems logical that I have to do the following:

-add a name to the form.
-add a new col to the SQL for the form name.
-change the bottom php tags to (near abouts (i'll check the correct syntax when I get back home)):

Code:
$getquery=mysql_query('SELECT id FROM comments 
                    ,SELECT name FROM comments
                    ,SELECT formName FROM comments
                    ,SELECT comment FROM comments
                        ORDER BY id DESC');
then I have to add in some if statements to the direct.php and the other stuff.

Now it's time to take a walk and think about this one for a bit.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 04:05 AM
So, before I make a damn fool of myself:

It may not be practical to use PHP in an HTML file. I ~could~ create a rule in the htaccess file to add PHP to the HTML, but wouldn't that just slow everything down?

It seems that it would be better to add a rule to the htaccess file to not display the page type on the browser window. Say I wanted to mess around later and convert everything to RoR, I won't have to worry about indexing and all that jazz. Why make my life harder than it has to be, right?

I'm going to continue on with my plan to externalize as much PHP as possible, but still have the appropriate pages with a PHP extension. I guess I am over-obsessed with non-obtrusive [programming to want to do anything different. Heck, I may learn something useful along the way.

As for step one, I changed the form to:

Code:
<form action="comment.php"
    method="POST">
    <fieldset>
        <legend>Comments:</legend>

        <p>This is commentTwo.php</p>
        <input type="hidden" name="subjectTwo">
        <p><label>Name:<br><input type="text" name="name"></label></p>
        <p><label>Comments:<br><textarea name="commentary"></textarea></label></p>
        
        <input type="submit" name="submit" id="submit" value="Hit me" />
        
    </fieldset>
</form>
Learned that


<form name="blah">

isn't legal syntax. I guess it's still legal in HTML4, but deprecated in XHTML and there doesn't seem to be anything that suggests this is proper in HTML5. Why they would do such a thing is beyond my comprehension. I guess they only want to raise the ceiling to trap out those not in the know. Interestingly, you can use

<fieldset name="blah">

HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 04:38 AM
The best way to hide the implementation detail from the visible URLs isn't to tell your web server to parse all HTML as PHP. Instead, use some sort of path rewriting scheme to direct your user-visible URLs to your internal files.

So yes, assuming Apache, use htaccess liberally.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 05:31 AM
Okay. I don't think I can actually delete anything from the htaccess file. I can add stuff to it apparently. Regardless, I sort of need to see the extensions for now, so on to my progress.

I added two rows to the comments table: subjectOne and subjectTwo, which via the invisible value will input either sOne or sTwo. (this doesn't actually happen at this point, but bear with me).

In the comment section, I put an if statement to select the subjectOne column and see if it is set to "sOne." This doesnt work either, but I think that is the right idea if I truly insist on keeping everything on one database table.

Apparently, this isn't common to do. Everything I have found that suggests seeing if there is a value in the table suggests that I am looking for some variable.

for example:

if(mysql_query("SELECT email FROM newUsers WHERE email='$email'")){

I also found this snippet:

if(mysql_num_rows(mysql_query("SELECT userid FROM plus_signup WHERE userid = '$userid'"))){
// Code inside if block if userid is already there
}


I have:

if(mysql_query("SELECT subjectOne FROM comments WHERE subjectOne='sOne'"))

It doesn't make any difference if I add the mysql_num_rows. Not that I expected something to turn out different.

Perhaps I can check to see if a value is empty?
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 06:07 AM
Code inside the if(mysql_query... will execute whenever the query doesn't error out.

Code inside the if(mysql_num_rows(mysql_query... will only execute when the query returns at least one row.

So there is a difference.

Edit: If you actually want to retrieve the values from the SELECT though, you should put the result of mysql_query into a variable.

Code:
$result = mysql_query([blah blah blah])

while($row = mysql_fetch_assoc($result))
{
[do stuff]
}
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 07:06 AM
Time for bed. This is my "progress" thus far:

Code:
<?php
require('connect.php');

if(isset($_POST['subjectTwo'])){
    $subjectTwo = $_POST['subjectTwo'];
}

if (isset($_POST['name'])){
    $name = $_POST['name'];
}

if (isset($_POST['commentary'])){
    $comment = $_POST['commentary'];
}

if (isset($_POST['submit'])){
    $submit = $_POST['submit'];
}

if($submit){
    if($name&&$comment){
        $insert=mysql_query("INSERT INTO comments (subject, name, comment) VALUES ('" . mysql_real_escape_string($subjectTwo) . "','" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($comment) . "')");
        header('Location: direct.php'); 
    }else{
        echo "Please fill out all fields";
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />



</head>

<body>

<form action="comment.php"
    method="POST">
    <fieldset>
        <legend>Comments:</legend>
        <p>This is commentTwo.php</p>
        <input type="hidden" name="subjectTwo" value="sTwo">
        <p><label>Name:<br><input type="text" name="name"></label></p>
        <p><label>Comments:<br><textarea name="commentary"></textarea></label></p>
        
        <input type="submit" name="submit" id="submit" value="Hit me" />
        
    </fieldset>
</form>


<?php

if(isset($_POST['subjectTwo'])){
    $subjectTwo = $_POST['subjectTwo'];
}

if(mysql_query("SELECT subject FROM comments WHERE subject='$subjectTwo'")){
$getquery=mysql_query('SELECT id, name, comment FROM comments ORDER BY id DESC');

while($rows=mysql_fetch_assoc($getquery)){
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];

    echo $name . '<br>' . $comment . '<br>' . '<br>';
    }
}

?>

</body>
</html>
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-05-2011 , 07:24 AM
Maybe I'm missing something, but why is it you put into a separate variable anything that you're pulling from an array? You do it with $_POST and then again with $rows.

Just legibility?
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-06-2011 , 12:55 AM
Wow, if this if that. Do I have to be so dense?

The solution was ldo obvious. I mean, I did study a little bit of SQL before.

SELECT .... FROM .... WHERE.... ORDER BY.... DESC....

I feel like there is a poem in there, but who has time for that?
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-06-2011 , 12:57 AM
Quote:
Originally Posted by Neil S
Maybe I'm missing something, but why is it you put into a separate variable anything that you're pulling from an array? You do it with $_POST and then again with $rows.

Just legibility?
You're probably missing the obvious: ['DELETE self-flame FROM this_post'];
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-06-2011 , 12:59 AM
I think you're right, so I checked if someone already wrote one.

http://michaeljswart.com/tag/sql-poem/
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-06-2011 , 01:35 AM
Quote:
Originally Posted by Neil S
Maybe I'm missing something, but why is it you put into a separate variable anything that you're pulling from an array? You do it with $_POST and then again with $rows.

Just legibility?
yeah there is really no good reason to do this.

also, i know you are wanting to learn from the ground up, which is great and all, but if you want to start building real db driven sites i'd recommend learning a framework asap.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
06-06-2011 , 01:36 AM
CodeIgniter is likely to be the least difficult to learn. I am a little biased because I use it for most of my projects, but it does have the least "magic" involved. Which is a good thing, IMO.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
HTML5/CSS3/advancedJavascript learning and programming logs
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
HTML5/CSS3/advancedJavascript learning and programming logs

      
m