Jump to content

Recommended Posts

Posted

Thanks for all of your help so far..........I am plugging away with mixed results.

 

At the moment I am following a tutorial at PHP MySQL Introduction

 

It works ok, but on this page: PHP MySQL Select it shows a PHP script which "displays results in a HTML table".

 

The tutorial does not tell me what to name this or where I should put the code or how I link to it. So far it tells me to create a persons.php file and an insert.php file which I have done.

 

Any suggestions would be welcome.

 

Cheers, Mike.

Posted (edited)
Thanks for all of your help so far..........I am plugging away with mixed results.

 

At the moment I am following a tutorial at PHP MySQL Introduction

 

It works ok, but on this page: PHP MySQL Select it shows a PHP script which "displays results in a HTML table".

 

The tutorial does not tell me what to name this or where I should put the code or how I link to it. So far it tells me to create a persons.php file and an insert.php file which I have done.

 

Any suggestions would be welcome.

 

Cheers, Mike.

 

If you are using xampp or apache or something along those lines I think all your web documents ie html, php, etc all go into the htdocs folder which should be where you installed either apache or xampp - when you know where you are putting your website documents you basically take the below php code

 

$con = mysql_connect("[b]localhost[/b]","[b]peter[/b]","[b]abc123[/b]");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db("[b]my_db[/b]", $con);

$result = mysql_query("SELECT * FROM [b]Persons[/b]");

echo [b]"[/b]</pre><table border="1">[b]
<[/b]tr>
Firstname
Lastname
";

while($row = mysql_fetch_array($result))
 {
 echo "";
 echo "" . $row['[b]FirstName[/b]'] . "";
 echo "" . $row['[b]LastName[/b]'] . "";
 echo "";
 }
echo "</table>";<br><br>mysql_close($con);<br>?&gt

localhost assuming it is setup on your pc can be localhost, 127.0.0.1 or if this is on a web server you can use the actual ip address assuming its a static ip address or the hostname of the server - if you want to work on a relevant port that you may have to configure it as such and when connecting to the database you would have either

 

hostname : port or I.P_Address : port

 

excluding spaces ie localhost:3260

 

peter - this is the username from the account you setup to access the database

 

abc123 - this is the password from the account you setup in this example it relates to the account peter

 

my_db - This is the name of your database so you will need to ammend this in the above code

 

Persons - This is the name of the table you are trying to access - again highlighted in bold in code snippet

 

FirstName - The first name field from your database, normally I don't include spaces in field names and type them exactly as is in the database

LastName - as per the first name field but obviously this is the last name field

 

You can have more fields and change the code where needed

 

Insert it into a notepad document or as per above suggestions notepad++ and save it as a dot php ie .php file extension and you put the saved php file into the htdocs folder - when it is inside of the htdocs folder you should be able to access it from your web browser ie

 

http://localhost/persons.php

 

The above assumes you named the php file persons.php

 

When executed assuming you have ammended the login credentials ( highlighted in bold in the above code snippet ) and everything else is setup correctly ie the database with the relevant table name, field names etc then it should work fine.

 

Apparently you should be able to include ( either using the include command if you have your php in seperate php files ) inside of a html document or literally putting the php inside of the html document using the php tags

// PHP Code goes in here
?> 

I have never managed to get it to work inside of a html document so if you figure that out let me know.

Edited by mac_shinobi
  • Thanks 1
Posted
Apparently you should be able to include ( either using the include command if you have your php in seperate php files ) inside of a html document or literally putting the php inside of the html document using the php tags

 

This is incorrect... you can't include php script and have it work in a .htm or .html file.

You can include a .php file, or .htm or .html file in a .php file.

 

The closest you'll get to 'including' a php file in an html file is by using an iframe, but that's nasty... avoid.

  • Thanks 1
Posted
This is incorrect... you can't include php script and have it work in a .htm or .html file.

You can include a .php file, or .htm or .html file in a .php file.

 

The closest you'll get to 'including' a php file in an html file is by using an iframe, but that's nasty... avoid.

 

Not true. If your web server is configure to parse files with the .html extension using PHP, then so be it. And in fact, any other arbitrary extension, which I do on a legacy site with some PHP but where it is essential the page names are preserved.

 

However, the overhead of passing every file through PHP just in case is high, which is why most hosts only parse .php/.php4/.php5/.phtml/.phps files.

  • Thanks 1
Posted
Yes... I should have worded that better... but in these guys cases we can safely assume that PHP is configured purely to parse the regular extensions...
  • Thanks 1
Posted

Once I have inputted data via the form below, named "persons.php", it sends data to the database, and then I get the page named "insert.php" coming up leaving the message: "1 record added".

 

What code do I need to put in the "insert.php" code to have it return to the "persons.php" page. I tried to put in:

include("persons.php"); but when I refreshed the page, it inputs the data into the database a second time.

 

..........................................................................................................................

 

"persons.php" code:

 

 

Firstname:

Lastname:

Age:

 

 

......................................................................................................................

 

 

"insert.php" code:

 

<?php

$con = mysql_connect("localhost","root","root");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

 

mysql_select_db("my_db", $con);

 

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

 

if (!mysql_query($sql,$con))

{

die('Error: ' . mysql_error());

}

echo "1 record added";

 

mysql_close($con)

?>

Posted
Not true. If your web server is configure to parse files with the .html extension using PHP, then so be it. And in fact, any other arbitrary extension, which I do on a legacy site with some PHP but where it is essential the page names are preserved.

 

However, the overhead of passing every file through PHP just in case is high, which is why most hosts only parse .php/.php4/.php5/.phtml/.phps files.

 

 

how would you do that as I re call when doing a project in classic asp I used asp inside of a html file ie

 




<% response.write "This is asp code" %>




Posted
Once I have inputted data via the form below, named "persons.php", it sends data to the database, and then I get the page named "insert.php" coming up leaving the message: "1 record added".

 

What code do I need to put in the "insert.php" code to have it return to the "persons.php" page. I tried to put in:

include("persons.php"); but when I refreshed the page, it inputs the data into the database a second time.

 

Replace:

 

echo "1 record added";

 

with:

 

header("Location: persons.php");

 

which will request the browser load the persons page. You must send headers before any other output, so you can't use echo statements before headers for example (unless they are conditional).

 

Note: please use the php tags (look in the toolbar in the edit box) to make it easier to read code.

  • Thanks 1
Posted
how would you do that as I re call when doing a project in classic asp I used asp inside of a html file ie

 




<% response.write "This is asp code" %>




 

You can nest PHP within HTML too, but I bet anything you like* that your classic asp scripts had a .asp extension. That's what tells the server to handle them specially instead of just spitting out what's on disk.

 

*disclaimer: "anything you like" in this context means "one small bar of chocolate no more than 35g in standardised weight and £0.50 in cost at the time of purchase". This offer is valid until 20/01/2010 and unclaimed offers expire after this time. No purchase necessary, no guarantee or indemnity is given whether express or implied. The promoter will not be held responsible for loss of items once dispatched to you.

  • Thanks 1
Posted

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

 

I realise this is a first bit of code but it's important to point out that you should never, ever, ever pass anything from your users straight into a database query like this, because this is how SQL injection happens. Somebody could come along and tell your form that their name is ';delete from users where 1;' or some other nice trick which lets them pass an arbitrary SQL query to your database.

 

There is a function mysql_real_escape_string() which you can pass all user provided information through and it will sanitise it so it can't do any damage.

 

So adding this before the query (or at the start of the script) will make it much safer:

 

$_POST[firstname]=mysql_real_escape($_POST[firstname]);

$_POST[lastname]=mysql_real_escape($_POST[lastname]);

$_POST[age]=mysql_real_escape($_POST[age]);

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...