Hello There, Guest!
View New Posts  |  View Today's Posts
Where's the error in this php script?

  • 0 Vote(s) - 0 Average


01-02-2015, 01:15 PM #1
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

Where's the error in this php script?
I hate to bother you guys again so quick, but I've tried everything and can't figure out why I'm unable write to a second column in my database. I followed everything to a tee in the example I've been following. Can any of you see where the error is? I've made a screenshot of the youtube video I'm following and my database as well as my php script and html form.





PHP Code:
<?php

define
('DB_NAME''myemaildata');
define('DB_USER''will777');
define('DB_PASSWORD''D241777');
define('DB_HOST''localhost');

$link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);

if (!
$link) {
        die(
'Could not connect: ' mysql_error());

}

$db_selected mysql_select_db(DB_NAME$link);

if (!
$db_selected) {
    die(
'Can\'t use ' DB_NAME ': ' mysql_error());

}

$value1 $_POST['RegName'];

$value2 $_POST['Email'];

$sql "INSERT INTO RegistrationList2 (RegName, Email) VALUES ('$value1', '$value2')";



if (!
mysql_query($sql)) {
       die(
'Error: ' mysql_error());

}

echo 
'Your email has been submitted. Thank you! Return to <a href="http://greendrinksforschools.info/contact/index.html">contact page</a> or <a href="http://greendrinksforschools.info/index.html">home page</a>.';

 

mysql_close();
?>


Code:
<form action="database.php" method="post" />
            <p>To receive blog posts by email, please submit your name and email address below.<br><br> Name<input type="text" name="RegName" /><br>Email<input type="text" name="Email" /><input type="submit" value="Submit" /></p>
            
            </form>
This post was last modified: 01-02-2015, 01:23 PM by william7.

01-02-2015, 02:52 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Where's the error in this php script?
I wouldn't be learning from this source if I were you:

- They use 'mysql_'
- They don't sanitize input to make the SQL query -- you'll get caught with SQLi vulnerabilities like this

Other than that, the syntax looks valid to me, why is all your HTML on a single line though?


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-02-2015, 05:30 PM #3
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Where's the error in this php script?
At first glance I'd also say the syntax looks valid, however as Ace pointed out do not use mysql_ anymore, it is deprecated. I strongly urge you to read through either of these:

- http://php.net/manual/en/pdo.prepared-statements.php
- http://php.net/manual/en/mysqli.quicksta...ements.php

After that write a class for this or use a pre-made one like the one I showed in a previous post of yours. It's good practice to try and write one yourself though, and if you make a good one it's re-usable in a lot of your projects.

01-02-2015, 07:30 PM #4
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

RE: Where's the error in this php script?
How exactly would this look? I just tried the modified script below and got the error message directly below.

Quote:Fatal error: Call to a member function prepare() on a non-object in /home1/william7/public_html/greendrinksforschools.info/contact/database.php on line 26

PHP Code:
$value1 $_POST['RegName'];

$value2 $_POST['Email'];

if (!(
$stmt $mysqli->prepare("INSERT INTO RegistrationList2(RegName, Email) VALUES ('$value1', '$value2')"))) {
    echo 
"Prepare failed: (" $mysqli->errno ") " $mysqli->error;


01-02-2015, 07:39 PM #5
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Where's the error in this php script?
Read through the entire thing. You get the error because you have not yet setup your MySQLi connection by using:

PHP Code:
$mysqli = new mysqli("example.com""user""password""database"); 

01-02-2015, 07:39 PM #6
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

RE: Where's the error in this php script?
(01-02-2015, 02:52 PM)AceInfinity Wrote:  I wouldn't be learning from this source if I were you:

- They use 'mysql_'
- They don't sanitize input to make the SQL query -- you'll get caught with SQLi vulnerabilities like this

Other than that, the syntax looks valid to me, why is all your HTML on a single line though?
I followed the You Tube example for the HTML form too. I would use the mysqli in place of mysql_ ; however, I don't know php. Even before I posted here on php, I saw a video about exactly what you're saying.

01-02-2015, 07:47 PM #7
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Where's the error in this php script?
(01-02-2015, 07:39 PM)william7 Wrote:  
(01-02-2015, 02:52 PM)AceInfinity Wrote:  I wouldn't be learning from this source if I were you:

- They use 'mysql_'
- They don't sanitize input to make the SQL query -- you'll get caught with SQLi vulnerabilities like this

Other than that, the syntax looks valid to me, why is all your HTML on a single line though?
I followed the You Tube example for the HTML form too. I would use the mysqli in place of mysql_ ; however, I don't know php. Even before I posted here on php, I saw a video about exactly what you're saying.

If you don't know PHP it's important you start doing the correct things immediately, don't use deprecated functions, they're deprecated for a reason. Reading up on the documentation on php.net is very good practice as you can learn a lot about what a function does and what it can be used for (some of the comments there are amazing).

01-02-2015, 07:55 PM #8
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

RE: Where's the error in this php script?
I tried what you pointed out, but I got the error below.

Quote:Warning: mysqli::mysqli(): (HY000/2005): Unknown MySQL server host 'DB_HOST' (0) in /home1/william7/public_html/greendrinksforschools.info/contact/database.php on line 8
Could not connect:

PHP Code:
<?php

define
('DB_NAME''myemaildata');
define('DB_USER''will777');
define('DB_PASSWORD''D241777');
define('DB_HOST''localhost');

$mysqli = new mysqli("DB_HOST""DB_USER""DB_PASSWORD""DB_NAME"); 


if (!
$link) {
        die(
'Could not connect: ' mysql_error());

}

$db_selected mysql_select_db(DB_NAME$link);

if (!
$db_selected) {
    die(
'Can\'t use ' DB_NAME ': ' mysql_error());

}

$value1 $_POST['RegName'];

$value2 $_POST['Email'];

if (!(
$stmt $mysqli->prepare("INSERT INTO RegistrationList2(RegName, Email) VALUES ('$value1', '$value2')"))) {
    echo 
"Prepare failed: (" $mysqli->errno ") " $mysqli->error;
}

$sql "INSERT INTO RegistrationList2 (RegName,Email) VALUES ('$value1','$value2')";



if (!
mysql_query($sql)) {
       die(
'Error: ' mysql_error());

}

echo 
'Your email has been submitted. Thank you! Return to <a href="http://greendrinksforschools.info/contact/index.html">contact page</a> or <a href="http://greendrinksforschools.info/index.html">home page</a>.';

 

mysql_close();
?>

01-02-2015, 08:06 PM #9
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Where's the error in this php script?
As I said before, read your error logs. It clearly says that it can't find a mysql server host "DB_HOST". This is not the value you are intending to give it and you should know that. The reason this is happening is because you are wrapping the names of your constants in quotation marks so they are actually strings.

Also you are not supposed to use mysql_select_db, you don't even have a mysql connection set up. Same goes for mysql_query.

01-02-2015, 11:50 PM #10
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Where's the error in this php script?
Just FYI william, you can't mix procedural PHP and OO PHP. You should stick with one or the other. Epixors is showing you OO style PHP (Object Oriented).


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  validation new to java script shaz2012 3 1,833 02-24-2013, 05:24 PM
Last Post: AceInfinity
  <?php error 1912Dummy 8 3,267 01-21-2013, 06:06 PM
Last Post: 1912Dummy
   Win7 x64 IIS OleDB Jet 4.0 Error KoBE 1 1,443 06-12-2012, 06:01 PM
Last Post: Nuno Brito
  [SCRIPT][PHP] PHP Highlighter w/ Line Numbers and php.net linkback Lee Stevens 4 2,917 03-19-2012, 05:08 AM
Last Post: Lee Stevens
  [SCRIPT][PHP] MySQL Backup Script. Lee Stevens 10 8,455 03-08-2012, 05:37 PM
Last Post: Lee Stevens


Users browsing this thread: 1 Guest(s)