0

Hello everyone.
I have juste started creating my website and I need some help.
I have found a thread about this subject but it have been dead since three months back so I didn't get what I wanted from there.

What I need help with is:
I want to create a form page (Let say page1.php) which I will use to post information to the database.
And I want the same page (page1.php) to automatically create a new page with the information from page1.php
On the new page I want to use a template that matches the rest of the website.

Can anyone please help me with the code that can do that?
I don't know where to start at this point, but I'm a quick learner.
Thank you.

3
Contributors
17
Replies
115
Views
3 Years
Discussion Span
Last Post by lukwagomedia
0

Start with w3schools they offer great tutorial for beginers. For every "step" you have example how to do that so that is starting point. If you want that your new page look like previus you need CSS template just insert few lines of codes and that is it.
I will start with funcionality how to insert into database and when you finish it then work on how site will look. If you have more questions feel free to ask...

0

Here is the code I use to post the form to the database.
What changes do I need to do here?

<?php
//initilize PHP

if ( isset ( $_POST['submit'] ) ) //If submit is hit
{

//No connect to the database.

mysql_connect("db_host", "db_username", "db_password"); 
 mysql_select_db("db_name") or die( "<p><span style=\"color: red;\">Unable to select database</span></p>");

//convert all the posts to variables.

$name = $_POST['name'];
$logo = $_POST['logo'];
$country = $_POST['country'];
$city = $_POST['city'];
$deadline = $_POST['deadline'];
$date_from = $_POST['date_from'];
$venue = $_POST['venue'];
$date_to = $_POST['date_to'];
$info_page = $_POST['info_page'];
$web = $_POST['web'];
$about = $_POST['about'];
$open_for = $_POST['open_for'];
$flag = $_POST['flag'];

//Insert the values into the correct database with the right fields.

mysql_query("INSERT INTO festivals (name, id, logo, country, city, deadline, date_from, venue, date_to, info_page, web, about, open_for, flag)".
"VALUES('$name', 'NULL', '$logo', '$country', '$city', '$deadline', '$date_from', '$venue', '$date_to', '$info_page', '$web', '$about', '$open_for', '$flag')") or die(mysql_error());
mysql_close();
//confirm
echo "<p><span style=\"color: red;\">Thank You; the form have been entered in the database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>";
}
else
{
// close php so we can put in our code
?>
 <form method="post" action="add.php">
-1

You can write something like this:

<form method=post action="page.php" id='my_form'>
<input type="hidden" id="userName" name="userName" value="login_id" >
<input type="hidden" id="password" name="password" value="some_password" >
</form>
  <script>
    document.getElementById("my_form").submit()
  </script>

This will make form with some input fields and then call submit so it will redirect to another page. If you want to send some data with that form you can do it like this:

<input type="hidden" id="username" name="username" value="<?php echo $_POST['username']; ?>" >

so you will have text filed filled with value from your previous POST and then that value will be send to another page...

0

Do not use mysql_* functions - use mysqli_* or PDO instead. If you insist on using mysql_*, then at least clean input variables ($_POST['*'] in this case), otherwise you will be open to SQL injection.

And I want the same page (page1.php) to automatically create a new page with the information from page1.php
On the new page I want to use a template that matches the rest of the website.

The whole point of php is that it can output dynamic data, so you do not have to create files. You can use the data from the DB to populate placeholders or variables within an exisiting page/template.

0

I do under standstand tha one and I have succeeded to post into db and even collect the information from it.
What I faild to do is: When I post that information, I want a specific page to be created on my server so that I don't have to that manually.

Take a look please: http://www.myfestivaldb.com/

On that page; every festival I add should have it's own information page.
I don't know if I'm clear enough but I hope you understand what I mean.

0

I understood perfectly. My point is that you manually create ONE template page (festivals.php). You use the url to draw data from the DB and place it in the respective places in the template.

SO for example

festivals.php?id=7

Would extract info from the DB based on festival#7 and place relevant data into the page:

if(isset($_GET['id']) && is_int($_GET['id']))
{
    $id = $_GET['id'];
    //run the query ...SELECT ... WHERE festival_id = $id
    //get data and place into $row array.
}else{
    //get the first record in the DB and use that
}

include "head.php"; //top of your themed pages

<h2>Festivals: <?php echo $row['title'];?></h2>
<p><?php echo $row['date'];?></p>
<p><?php echo $row['description'];?></p>

include "footer.php"; //bottom of your themed pages
0

Thanks.
I'm on the right track now. But I'm a bit confused here in your code.

}else{
//get the first record in the DB and use that
}

could you please close the code so I can see how it looks like.
I mean as following: (Note that I use your own code with no changes).

    <?php
    if(isset($_GET['id']) && is_int($_GET['id']))
    {
    $id = $_GET['id'];
    //run the query ...SELECT ... WHERE festival_id = $id
    //get data and place into $row array.
    }else{
    //get the first record in the DB and use that
    }
    include "head.php"; //top of your themed pages
    <h2>Festivals: <?php echo $row['title'];?></h2>
    <p><?php echo $row['date'];?></p>
    <p><?php echo $row['description'];?></p>
    include "footer.php"; //bottom of your themed pages

    ?>

Should it look like that?
Thank you once again.

0

The if control structure is there as a catch-all, in case an id is not given in the url. So in fact it could look like this...

$db = mysqli("localhost","root","","daniweb");
if(isset($_GET['id']) && is_int($_GET['id']))
{
    $id = $_GET['id'];
    $result = $mysqli->query("SELECT ... WHERE festival_id = $id";
    $num = $result->num_rows;
    if($num)
    {
       $row = $result->fetch_assoc(); 
    }
}
if(!isset($_GET['id']) || (isset($num) && !$num))
{
    $id = 1; //default record to show if a problem
    $result = $mysqli->query("SELECT ... WHERE festival_id = $id";
    $num = $result->num_rows;
    if($num)
    {
       $row = $result->fetch_assoc(); 
    } 
}

Sorry that looks a bit spaghetti-fied, but it could be something like that.
All you festival data is now in the $row array, ready to be placed in situ in the template markup.

0

So. First I want to thank you for your help. Thank you.

Now, I have spent all the daytrying to get my code work.
But something is still not right.
I think i might be duplicationg some of the code or just coding wrong.
Because when i visit that page on the net it doesn't show at all.

My code wa way to long, so I cut it down to a few field.
Please let me know what I'm doing wrong here:

<table width="990" border="0" align="center" cellpadding="0" cellspacing="0" class="mainnews-table">
  <tr>
    <td bgcolor="#FFFFFF" class="mainnews-inner-table">
    <table width="100%" border="0" cellpadding="2" cellspacing="2" class="mainnews-inner-table">
      <tr>
        <td align="left" valign="top" class="main-header-td">

          <table width="100%" border="0" cellspacing="3" cellpadding="3">
            <tr>
              <td> <div class="mainnews-header">Add festival form.<div style="float:right"><a href="http://www.myfestivaldb.com/add.php">Add a new festival</a></div></div></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td width="228" align="left" valign="top">
<?php
// Lets connect to the database
$con =  mysqli_connect("db_host", "db_user", "db_pass", "db_name");
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: (" . $mysqli_connect_errno . ") " . $mysqli_connect_error;
}
if(isset($_GET['id']) && is_int($_GET['id']))
{
    $id = $_GET['id'];
    $result = $mysqli->query("SELECT festivals WHERE id = ?");
    $num = $result->num_rows;
    if($num)
    {
        $row = $result->fetch_assoc();
    }
}
if(!isset($_GET['id']) || (isset($num) && !$num))
{
    $id =1; //Default record to show if a problem.
    $result = $mysqli->query("SELECT festivals WHERE festival_id = $id");
    $num = $result->num_rows;
    if(sum)
    {
        $row = $result->fetch_assoc();
    }
{
// If submit is hit
 if(!isset($_POST['sumbit']))
{
// Convert all the posts to variables.
$name = $_POST['festival_name'];
$city = $_POST['city'];
$deadline = $_POST['deadline'];
$url = $_POST['url'];

//Store template placeholders in an array.
$placeholders = array("{festival_name}", "{city}", "{deadline}", "{url}");

// Get template.php as string.
$tpl = file_get_contents($tpl_patch.$tpl_file);
$new_festival_file = str_replace($placeholders, $data, $tpl);

//Now create a new file. We use the festival name to create a new file.
$php_file_name = $data['festival_name'].".php";

//Now lets write this to the festivals directpry.
$fp = fopen($festivals_path.$php_file_name, "w");
fwrite($fp, $new_festival_file);
fclose($fp);

//Insert the values into the correct database with the right fields.
mysqli_query($connection, "INSERT INTO festivals (id, festival_name, city, deadline, url) VALUES (?'festival_name', 'city', 'deadline', 'url')");

echo "The added festival\'\s name is: <b>$festival_name</b>   '' and has this id:" . mysqli_insert_id($con);

mysqli_close();
}
?>
 <form method="post" action="add.php" id='festivals'>
 <!---form-->
<table width="100%" border="0" cellspacing="2" cellpadding="2" id="contact-form">
          <tr>
            <td colspan="2"><h4>Use this form to add titles.              <br>
              All forms are required.</h4><br></td>
            </tr>
          <tr>
            <td width="162" align="right">
            <strong>Festival name:</strong>
            </td>
            <td width="804">
            <input name="festival_name" type="text" autofocus required id="festival_name" tabindex="1">


            </td>
          </tr>
          <tr>
            <td align="right"><strong>City:</strong></td>
            <td>
              <span class="value">
  <input type="text" required id="city" placeholder="Enter festival city" tabindex="3" name="city" ></span>
  </td>
          </tr>
          <tr>
            <td align="right"><strong>Deadline</strong></td>
            <td>
              <input name="deadline" type="text" id="deadline" tabindex="8" value=""> 
              </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Webpage:</strong></td>
            <td>
              <input name="url" type="text" required id="url" placeholder="Begin with http://" tabindex="11" value="">
              </td>
          </tr>
          <tr>
            <td align="right" valign="top">&nbsp;</td>
            <td>
              <button type="submit" name="submit" id="contact-submit">Submit the form</button>

              </td>
          </tr>
          </table>
          <!--/form-->
          </form>
           <script>
document.getElementById("festivals").submit()
</script>
          <?php
}
} //close the else statement
?>

Thank you again.

0
$result = $mysqli->query("SELECT festivals WHERE id = ?");

This is for prepared statements and binding parameters - you haven't done either.

0

I feel ashamed sayoing this, but I have no clue how that could be done. Even though I'm spending many hours learning to code.

Could you please give me an example on how I can do that?
You don't have to if you got no time but I will appriciate it and keep on learning.
You can use the code above.
Thank you very much.

0

Again, I don't know why you're creating a physical file for each festival - it doesn't make sense. You just access each one like this...

www.example.com/festival.php?id=7
www.example.com/festival.php?id=19

By using mod_rewrite in Apache, you could even get these urls to look something like...

www.example.com/festivals/7/knebworth-2005
www.example.com/festivals/19/monsters-of-rock-castle-donnington-1984

But anyway, they'd all resolve to give you a GET id, which could be used in the following fashion...

<?php
$db = mysqli("localhost","root","","daniweb");
$id = (isset($_GET['id']) && is_int($_GET['id'])) ? $_GET['id'] : 1;
$result = $mysqli->query("SELECT ... WHERE festival_id = $id LIMIT 1");
$num = $result->num_rows;

if($num)
{
    $festival = $result->fetch_assoc();
    $_error_msg = ''; 
    $_page_title = $festival['title'];
    $template = 'festival.php';
}else{
    $_error_msg = "<p class='error'>Sorry, this festival doesn't exist. Choose from the list below:</p>"; 
    $_page_title = "No Festival Chosen";
    $template = 'nofestival.php';
}

$page_title = 'Festivals: ' . $_page_title;
$description = 'This is a description of the page for the meta content';

require 'templates/header.php';
require 'templates/sidebar.php';
require 'templates/' . $template;
require 'templates/footer.php';
?>

The template/festival.php file could be something like this...

<div id="festival">
    <h3><?php echo $festival['title']'?></h3>
    <h4><?php echo $festival['date']'?></h4>
    <p class="description"><?php echo $festival['title']'?></p>
</div>

But I think I've already mentioned that.

0

Thank you very much.
But I'm still fighting with this one, I will let you know when I succeed or fail ;)

0

I have now tried and tried several times to make sure that it really doesent work before I ask again.
I remember you told me that I have to use mysqli instead just myaql.
And that is exaktly the error in it all.

whenever I use mysqli, my website just go blank and I recieve an error message saying: Access denied for user ''@'web103.local.one.com' (using password: NO).

So I think that I cannot get your code to work because it is written with mysqli_queries.

But thank you very much anyway.

Edited by lukwagomedia

0

The problem is now solved.
Thank you very much everyone and mostly "diafol" for helping me out.
This is the code I used in case anyone else want to use it.

<?php
include_once('db_connection.php');

if( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Id from list_page.php

$id = $_GET['id'];

} else {
    // If no valid ID, stop the script  
echo '<p class="error">You did not select anny page(Or whatever)</p>';

}
include_once('../config/config.php');

// Select the record
$q = "SELECT * FROM festivals WHERE id = $id LIMIT 1";
$result = @mysqli_query($dbcon, $q);
if (mysqli_num_rows($result) == 1) {
     // Valid user ID, display the form.

// Get the user's information
$row = mysqli_fetch_array ($result, MYSQLI_ASSOC);
// Create the form
echo '

<table>
<tr>
<td>

// Your page content goes here.
// E.g

'.$row['first_name'].'

// And so on


</td>
</tr>
</table>';

mysqli_close($dbcon);
}
?>

And as diafol mentioned. The link in this case should look like this:
<a href="to_template_page.php?id='.$row['id'].'">View item page</a>

Thank you again.
www.myfestivaldb.com to see the code in action.

Edited by lukwagomedia: wrong spelling

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.