10 Replies - 730 Views - Last Post: 05 April 2009 - 10:23 PM Rate Topic: -----

#1 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Need Help with simple php script using MySQL

Post icon  Posted 04 April 2009 - 02:32 PM

The assignment can be found here

I want to know how it is possible to do the forms in php to submit it to the database?
Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with simple php script using MySQL

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need Help with simple php script using MySQL

Posted 04 April 2009 - 03:00 PM

First start coding by the form layout.
The form is to be posted on the same page.
When the form is not filled, it should show the empty form layout and after it is filled and posted, it should show the table. Here use if-else statement.
here is sample-
<?php
if (isset($_POST[posted])) {
   Show the table
   And a link to insert more data
} else {
?>
<h2>Please fill out this form</h2>
<form method="POST" action="<?php $PHP_SELF ?>">
<input type="hidden" name="posted" value="true">
Form Data
<input type="submit" value="Send Info">
</form>
<?php
}
?>


Was This Post Helpful? 1
  • +
  • -

#3 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Need Help with simple php script using MySQL

Posted 04 April 2009 - 03:02 PM

thanks, I wasn't sure if i was allowed to used html in a php script, to do the forms, well its good to know now.
Was This Post Helpful? 0
  • +
  • -

#4 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need Help with simple php script using MySQL

Posted 04 April 2009 - 03:05 PM

Try this way. I think you can do it.
Was This Post Helpful? 0
  • +
  • -

#5 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:10 PM

Ok i have the form, down, but now i'm having trouble submitting it to the database, Here is what i have so far

<?php

// GET?

if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
 // Show form

echo ("<html>");
echo ("<head></head>");
echo ("<body>");

echo("<h1>New Course</h1>");
echo("Enter a new course");
echo ("<br><br>");

echo("<form method = POST  action = courses.php>");
echo("ID:   ");
echo("<input type = text name = ID ");
echo("<br>");

echo ("Dept:");
echo("<input type = text name = Dept ");
echo("<br>");

echo ("Num:");
echo("<input type = text name = Num ");
echo("<br>");


echo ("Title:");
echo ("<input type = text name = title ");
echo("<br>");


echo ("Instructor:");
echo("<input type = text name = instruct ");
echo("<br>");


echo("<input type =submit value = save>");






echo("</body>");
echo ("</html>");

}
else
{
 // POST

$hostname = 'taz';
$username = 'fmccown';
$password = 'frank';

mysql_connect($hostname, $username, $password);


$database = 'fmccown';
mysql_select_db($database);


// Insert

$query = "INSERT INTO courses VALUES ($_POST[courseid], $_POST[dept] ...etc...";



$query = "SELECT * FROM courses order by dept";
$result = mysql_query($query) or die(mysql_error() . "<br>query = $query<br>");

print "<table border=1>";

// Same thing using an associative array
while ($row = mysql_fetch_array($result))
  echo "<tr><td>$row[courseid]</td><td>$row[dept]</td><td>$row[num]</td></tr>\n";

print "</table>";
print "<p>That's it.</p>";

}



Was This Post Helpful? 0
  • +
  • -

#6 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:45 PM

It seems you are passing data to a new page (courses.php). You need to submit the data to itself.Use
echo("<form method = POST  action='$_SERVER[PHP_SELF]' ");


Instead of
echo("<form method = POST  action = courses.php>");


Was This Post Helpful? 0
  • +
  • -

#7 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:46 PM

Well this page what i am working on now is courses.php, but i will try it.

This post has been edited by Zerobu: 05 April 2009 - 06:47 PM

Was This Post Helpful? 0
  • +
  • -

#8 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:52 PM

If you want to pass data to courses.php page, then you need to use sql statements at that page. Or you can just submit the page on itself.
Was This Post Helpful? 0
  • +
  • -

#9 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:58 PM

what i am trying to do, is submit this data to a table in a database and then display that table
like in the example

I'm having trouble with the SQL statements to insert tables and place data in the table

This post has been edited by Zerobu: 05 April 2009 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 06:58 PM

You can do it with both at the same page or passing data to a new page. If you want to pass data to new page, then you need to show only the form at first page and using post method get the data at new page and then using sql statements to insert these data to database. After that use sql statements to retrieve data from database and show the data at a table.
Was This Post Helpful? 0
  • +
  • -

#11 TechWar  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 470
  • Joined: 17-February 09

Re: Need Help with simple php script using MySQL

Posted 05 April 2009 - 10:23 PM

Are you saying you need to know the SQL? or the PHP?
Like SQL to create a table
CREATE TABLE tablename (varname1 VARCHAR(10), varname2 VARCHAR(10), varID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY)



or do you want PHP or both?

This post has been edited by TechWar: 05 April 2009 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1