-1

Okay i have a code for Add and i can put the details in but after i click on Add for some reason i think it doesnt save to the database and just gives me a blank page this is my adding php code

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($id, $tit, $sal, $loc, $desc, $cat, $error)
 {
 ?>

 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

 <form action="" method="post">
 <div>
 <strong>ID: *</strong> <input type="text" name="id" value="<?php echo $id; ?>" /><br/>
 <strong>TITLE: *</strong> <input type="text" name="title" value="<?php echo $tit; ?>" /><br/>
 <strong>SALARY: *</strong> <input type="text" name="salary" value="<?php echo $sal; ?>" /><br/>
 <strong>LOCATION: *</strong> <input type="text" name="location" value="<?php echo $loc; ?>" /><br/>
 <strong>DESCRIPTION: *</strong> <input type="text" name="description" value="<?php echo $desc; ?>" /><br/>
 <strong>CATEGORY: *</strong> <input type="text" name="category" value="<?php echo $cat; ?>" /><br/>
 <p>* required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 


 <?php 
 }


 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['Add']))
 { 
 // get form data, making sure it is valid
 $id = (htmlspecialchars($_POST['id']));
 $title = (htmlspecialchars($_POST['title']));
 $salary = (htmlspecialchars($_POST['salary']));
 $location = (htmlspecialchars($_POST['location']));
 $description = (htmlspecialchars($_POST['description']));
 $category = (htmlspecialchars($_POST['category']));

 // check to make sure both fields are entered
 if ($id == '' || $title == '' || $salary == '' || $location == '' || $description == '' || $category == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($id, $title, $salary, $location, $description, $category, $error);
 }
 else
 {
 // save the data to the database
 $pdo->query("INSERT jobs SET id='$id', title='$title', salary='$salary', description='$description', category='$category'")
 or die; 

 // once saved, redirect back to the view page
 header("Location: securepage.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','');
 }
?>

i cant to figure out what is wrong and as its not showing me any errors its more difficult. But i think im sure it has something to do with the not saving to the database after Inserting. I hope someone can help me out.

my database is called vacancies and table is jobs this are the values in the table id, title, salary, location, description, category_id

please ask me if you need further details to help me out better

Nina555
  • 15
  • 6
  • Error_reporting add on top – devpro Jan 17 '16 at 04:03
  • Insert into table (column..) values (values ...) this is the right way.. well yur query will also work. But ist add error reporting on.. and than print yur query and run manually in phpmyadmin – devpro Jan 17 '16 at 04:06
  • PHP usually logs errors to the "error log" file. Depending on how it's configured, errors might also show in the Web page (good for development) or not (good for public sites). You're seeing a blank page instead of errors, so you'll need to look in the log. – RJHunter Jan 17 '16 at 04:06
  • I can't see why this answer http://stackoverflow.com/a/34834964/1415724 was accepted, seeing that isn't the full solution. There's bigger problems here. – Funk Forty Niner Jan 17 '16 at 04:45

2 Answers2

1

Inside your form tags you need to add a hidden input with Add name attribute.

<input type="hidden" name="Add" value="1" />

This is because you have an if statement checking for it.

if(isset($_POST['Add']))
plhyhc
  • 121
  • 1
  • 6
1

Replace this line

if (isset($_POST['Add']))

With

if (isset($_POST['submit']))

Side note:

On error_reporting at top of the page.

Second issue is that as I mentioned in comments if you are using INSERT query in MYSQL not SQL than you can follow this example:

INSERT INTO table (column1, column2...) VALUES (value1, value2...)
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Look at their query `INSERT jobs SET` ;-) isn't the only thing failing them. Hard to say what they want to do here though. INSERT? UPDATE? – Funk Forty Niner Jan 17 '16 at 04:43
  • (INSERT jobs.*, categories.category_name FROM jobs LEFT JOIN vacancies.categories ON categories.category_id = jobs.category_id' SET id='$id', title='$title', salary='$salary', description='$description',category='$category_name'") would this be better as i usually use this but i tried if it works if i just put the table name in and it still works like that BUT just one row is not showing for some reason – Nina555 Jan 17 '16 at 04:50
  • @fred-ii .. hi friend yes I notice and mentioned in comments – devpro Jan 17 '16 at 04:51
  • 1
    @Nina555 that isn't even in your question. You really need to read the manuals on MySQL syntax http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-manipulation.html – Funk Forty Niner Jan 17 '16 at 04:51
  • 1
    @devpro seeing your edit, their syntax looks 95% UPDATE. As I stated above, it's hard to say if they want to INSERT or UPDATE. Seeing what they posted, again; that is UPDATE syntax. You'll need to show them both methods ;-) – Funk Forty Niner Jan 17 '16 at 04:59
  • @devpro addendum to above. However, they don't have a `WHERE` clause if they want to UPDATE. If so, then that will update their entire db. – Funk Forty Niner Jan 17 '16 at 05:00
  • Yes very important and than OP will post a new question how to recover my data :p @fred-ii – devpro Jan 17 '16 at 05:02
  • 1
    @devpro It's *back to the drawing board* for them ;-) – Funk Forty Niner Jan 17 '16 at 05:03
  • Now I highly recommend u to post yur answer as a senior I knw u will guide him or her. Waiting @fred-ii ... and I knw u are the genius. :p – devpro Jan 17 '16 at 05:06