Jump to content

learning php


j05hr

Recommended Posts

  • Replies 115
  • Created
  • Last Reply

bump for Nightslyr

 

Hey, sorry about that.  Some personal things have come up, so my time's been limited.

 

I'll be able to help you more fully tomorrow, but in the meantime, a couple of things:

 

1. Create a new database column named 'id'.  Make it an unsigned int (4) non-null auto-increment primary key.  I always like having a generic id field, just in case.

 

2. You'll need to test your PHP script database connection.  So, in notepad, copy the following and name it dbtest.php:

<?php
   require_once('../../dbconnect.php');

   $dbc = mysql_connect(HOST, USER, PASS) OR die("Unable to connect to database: " . mysql_error());
   mysql_select_db(MYDB) OR die("Unable to select database: " . mysql_error($dbc))
   
   $query = "INSERT INTO jason_gold (name, email_address) VALUES ('Kevin', 'kevin@gmail.com')";
   $result = mysql_query($query);

   if(mysql_affected_rows() == 1)
   {
      echo "Insert successful!";
   }
   else
   {
      echo "Something went wrong!";
   }
?>

 

Upload that into your web root folder, then access it through your web browser by visiting that file (it should be at www.jasongold.org/dbtest.php).  Report back all the output and/or errors displayed on the screen.

Link to comment
Share on other sites

sorry to hear that, hope everythings ok.

 

i'm not sure i'll be around tomorrow as i have to go in to work, but for now i tried to do the id field and got this error back

 

SQL query:

 

ALTER TABLE `name` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST

 

MySQL said: Documentation

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

 

i'm not sure how to correct it.

Link to comment
Share on other sites

i've fixed it now and made the php test page and it comes up blank so i assume that means it works as there's no errors?

 

so ready for the next step unless of course that is wrong that it comes up blank

 

Not seeing anything when visiting that file is bad.  Something went wrong somewhere.

 

Do me a favor and add the line:

error_reporting(E_ALL);

 

To the file after the line with require_once().  Upload it to your web root, and then visit it again.  Hopefully it'll output the error(s) causing the problem.

 

Also, check the database itself to see if anything's been inserted into it.  There should be at least one row with information in it.

 

Are any of the other columns not-null?

Link to comment
Share on other sites

just tried everything that i could think of, there is know information in the database, it says: MySQL returned an empty result set (i.e. zero rows). (Query took 0.0011 sec)

 

and id should be the only column not-null and the rest should be null?

 

when i upload the files to my web root that does mean to put it in the folder with all my php?

 

and on the connect page it should be localhost and root i'm connecting to?

 

and the problem is still that the page came up blank

Link to comment
Share on other sites

Hello, I'm new to PHP and getting started on Array Functions...

 

Whenever I try to execute the array_intersect_key() function

 

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => ;

var_dump(array_intersect_key($array1, $array2));

?>

 

I get this error:

 

Fatal error:  Call to undefined function array_intersect_key() in D:\Programmers\Caroline\array_pract3.php on line...

 

Can someone tell me why it is so - even with array_intersect_key being a valid PHP function? ???

 

 

TYIA,

Caroline

Link to comment
Share on other sites

just tried everything that i could think of, there is know information in the database, it says: MySQL returned an empty result set (i.e. zero rows). (Query took 0.0011 sec)

 

and id should be the only column not-null and the rest should be null?

 

when i upload the files to my web root that does mean to put it in the folder with all my php?

 

and on the connect page it should be localhost and root i'm connecting to?

 

and the problem is still that the page came up blank

 

Okay, let's start at the beginning.

 

First, your web root would be the folder that you'd put all of your website files in. It's the folder that is visible to the internet. Normally, it's named something like public_html, but not always. It's the folder where you put all your HTML files, and is where you should put all of your PHP files, except your dbconnect.php file. You can create other folders within the web root to help organize things, but all the files you want to show to the outside world have to be in this folder somewhere.

 

Where is your dbconnect.php file located in relation to your other website files? I think you said you put it two folders above the web root. Is this correct? For that file, unless you were given a path to the database not named localhost, then all you need for the host value is localhost (I hope that made sense). Again, the dbconnect.php file should simply be:

<?php
   DEFINE('HOST', 'localhost');
   DEFINE('USER', 'username');
   DEFINE('PASS', 'password');
   DEFINE('MYDB', 'database name');
?>

 

Where 'username' and 'password' are the login info to the database, and 'database name' is the name of the database you need to access.

 

Double-check with your hosting that 'localhost' is all you need. My own hosting requires me to enter in more than just 'localhost' when I'm using straight mysql, so you may be running into the same problem.

 

Regarding the database columns: setting one to not-null means that it is not valid to leave that column empty. Your id column should be not-null because every database row should have an id associated with it. For the other columns, it all depends on whether or not you want to force your users to enter certain information. There's no real right or wrong answer to this, as it depends on what columns you don't mind potentially leaving empty.

 

You should probably show me the code for both PHP files, just so I can double-check what's going on. I just ran a test on my hosting, which worked fine, so I'm thinking you either have a syntax error, bad database login info, or the dbconnect file is in the wrong spot and you're not actually including it.

 

EDIT: I think I see the problem. I just looked at your table structure again, and all columns are set to not-null. Since that's the case, change the line with $query = "INSERT INTO..."; to:

$query = "INSERT INTO name (name, email_address, day_number, night_number, best_way_to_reach, type_of_event, date_of_event, budget, how_did_you_find, comment_questions) VALUES ('Kevin', 'kevin@gmail.com', 0, 0, 'email', 'party', '2008-12-25', '1200.50', 'phpfreaks', 'great site')";

 

And see if it works now.

 

EDIT2: Make sure you highlight the entire line I wrote above...the code box cut it off, but you should be able to get the whole thing if you keep dragging your mouse to the right.

Link to comment
Share on other sites

my html files are located in /var/www/html i put all the files there except dbconnect.php which i put in just /var

 

i've written to the hosting company about what the login would be for using mysql.

 

i understand about the null stuff now and have taken them off except for id so don't need to add that code in?

 

dbtest.php

<?php
   require_once('../../dbconnect.php');
error_reporting(E_ALL);
   $dbc = mysql_connect(HOST, USER, PASS) OR die("Unable to connect to database: " . mysql_error());
   mysql_select_db(MYDB) OR die("Unable to select database: " . mysql_error($dbc))
   
   $query = "INSERT INTO jason_gold (name, email_address) VALUES ('Kevin', 'kevin@gmail.com')";
   $result = mysql_query($query);

   if(mysql_affected_rows() == 1)
   {
      echo "Insert successful!";
   }
   else
   {
      echo "Something went wrong!";
   }
?>

 

dbconnect.php

<?php
   DEFINE ('HOST', 'localhost');
   DEFINE ('USER', 'root');
   DEFINE ('PASS', 'example');
   DEFINE ('MYDB', 'jason_gold');
?>

 

is there anything else i need to put in my directory except for those two files to make it work? i thought i had to load a file called config from the wamp folder? although i'm probably completely wrong

Link to comment
Share on other sites

Hmm...well, the files both look okay, and removing the not-null from the other columns will definitely make testing easier.  I'm unsure if you need to include that WAMP config file, as I've only ever used *nix hosting for my PHP projects.

 

Did you try testing it after you modified the column attributes?  Because everything else looks right.  The only things I could think would be causing nothing to happen is either a bad db host name, or that it can't 'see' the dbconnect file due to file permissions.  That said, you should still be seeing some errors on the screen if that's the case.  The calls to the database would generate several errors if it couldn't connect.

 

Hmm...did you ever need to change file permissions for your normal HTML files to be visible?  Because you shouldn't be seeing a blank, white screen regardless of whatever errors are there.  You should be seeing something.

 

I probably won't be around much this weekend.  I'm not usually on the computer at all during the weekend, so keep at it, and feel free to ask others questions.

Link to comment
Share on other sites

so when you first upload a php file all you do is put the connect file two folders above the web root one?

 

yeah i tried testing it after modifying it and still nothing.

 

i never needed to change file permissions for my normal html so can't see it being that as a problem

 

thanks for the help, i've written to my hosts so hopefully they will be able to fix it and then hopefully be able to get going again on monday

Link to comment
Share on other sites

Hello, I'm new to PHP and getting started on Array Functions...

 

Whenever I try to execute the array_intersect_key() function

 

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => ;

var_dump(array_intersect_key($array1, $array2));

?>

 

I get this error:

 

Fatal error:  Call to undefined function array_intersect_key() in D:\Programmers\Caroline\array_pract3.php on line...

 

Can someone tell me why it is so - even with array_intersect_key being a valid PHP function? ???

 

 

TYIA,

Caroline

Possibly your version of php is 4.x?  This function only exists in php5+

Link to comment
Share on other sites

after playing around with it all weekend i managed to get it to work but not all is good, it came back with the message something went wrong

 

Hmm...so now it looks like the insert query itself isn't working.

 

Can you repost the main script code?  I just want to see how I handled the errors.  I think I can add some more error reporting so it can point us in the right direction to fixing this new problem.

Link to comment
Share on other sites

do you mean this one?

 

dbtest.php

<?php
   require_once('../../dbconnect.php');
   error_reporting(E_ALL);
   $dbc = mysql_connect(HOST, USER, PASS) OR die("Unable to connect to database: " . mysql_error());
   mysql_select_db(MYDB) OR die("Unable to select database: " . mysql_error($dbc))
   
   $query = "INSERT INTO jason_gold (name, email_address) VALUES ('Kevin', 'kevin@gmail.com')";
   $result = mysql_query($query);

   if(mysql_affected_rows() == 1)
   {
      echo "Insert successful!";
   }
   else
   {
      echo "Something went wrong!";
   }
?>

Link to comment
Share on other sites

Yup, that's the one.

 

Hmm... try changing the '$result = ...' line to:

$result = mysql_query($query) OR die("Insert query error: " . mysql_error());

 

That will kill the script if the insert doesn't work and, hopefully, spit out a useful error message.

Link to comment
Share on other sites

Insert query error: Table 'jason_gold.jason_gold' doesn't exist

 

Aha!  Good, you nailed down the problem.

 

Change the '$query = ...' line to:

$query = "INSERT INTO name (name, email_address) VALUES ('Kevin', 'kevin@gmail.com')";

 

The reason for this is that your database is named 'jason_gold', but your table name is simply 'name'.

Link to comment
Share on other sites

Haha i did that because it asks what field you want to call it so i thought it meant the first record, i'll know for next time. It worked, so do we do the code for the form next?

 

Now that it worked, you should erase the values from the database.  You don't need test data floating around in there any more.

 

I'm going to have to look at what you have for a form again, then write the rest of the script.  I'm thinking of just doing it wholesale as that'll be more efficient, so hopefully you'll have a complete script ready to go tomorrow.  I'll just post it in this thread when I'm done.  I'll also explain the different steps involved in handling submitted data.  So, be on the lookout for a rather long post or two.

Link to comment
Share on other sites

done, does anything from best_way_to_reach have to be changed as it's a drop down menu?

 

Nope, that column is fine.

 

One more thing: I'm trying to save myself a bit of work by using the mysqli extension (it literally stands for mysql improved), but I'm not sure if your hosting has it.  So, write the following script:

<?php
   php_info();
?>

 

Name that file info.php, then navigate to it.  Once there let me know what version of PHP you're using and if you have the mysqli extension.  DON'T post a screenshot, and erase that script when you're done.  It'll expose some sensitive info that could potentionally be used against your server.

Link to comment
Share on other sites

Just curious, what info exposed could really compromise security? Sure, a hacker would have more information on hand about my server, but I only see this being a problem if my software is out of date and known exploits can be determined...oh, I guess that it is a security risk.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.