-1

Good morning/afternoon,

Can someone please explain to me why i keep getting a;

Warning: Missing argument 4 for renderForm(), called in /home/***/public_html/new.php

Here is the HTML/PHP code i am using;

<?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($date, $home, $time, $away, $city, $error)

{

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>New Record</title>

</head>

<body>

<?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>

<label>Date:</label>
<input class="input" name="id" type="text" value="<?php echo $date; ?>">
<br/>

<label>Home Team:</label>
<input class="input" name="id" type="text" value="<?php echo $home; ?>">
<br/>

<label>Time:</label>
<input class="input" name="id" type="text" value="<?php echo $time; ?>">
<br/>

<label>Away Team:</label>
<input class="input" name="id" type="text" value="<?php echo $away; ?>">
<br/>

<label>Location:</label>
<input class="input" name="id" type="text" value="<?php echo $city; ?>">
<br/>


<p>* required</p>

<input type="submit" name="submit" value="Submit">

</div>

</form>

</body>

</html>

<?php

}









// connect to the database

include('connect-db.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['submit']))

{

// get form data, making sure it is valid

$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));

$home = mysql_real_escape_string(htmlspecialchars($_POST['home']));

$time = mysql_real_escape_string(htmlspecialchars($_POST['time']));

$away = mysql_real_escape_string(htmlspecialchars($_POST['away']));

$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));



// check to make sure both fields are entered

if ($date == '' || $home == '' || $time == '' || $away == '' || $city == '')

{

// generate error message

$error = 'ERROR: Please fill in all required fields!';



// if either field is blank, display the form again

renderForm($date, $home, $time, $away, $city, $error);

}

else

{

// save the data to the database

mysql_query("INSERT data SET date='$date', home='$home', time='$time', away='$away', city='$city'")

or die(mysql_error());



// once saved, redirect back to the view page

header("Location: view.php");

}

}

else

// if the form hasn't been submitted, display the form

{

renderForm('','','');

}

?>

I have tried to search here and google with no fix. Can anyone please help me please? This should be a form to add new events. But with these errors i am unable to make it work. Thank you in advance for any assistance you might be able to provide.


The original part is done. Thank you for all the answers. But now i after i submit it gives me this error, not sure if it has anything to do with the edit i made to:

renderForm(null,null,null,null,null,null);

Thank you once again.

Joe
  • 83
  • 1
  • 1
  • 7
  • 1
    `renderForm('','','');` down the bottom of your code is missing a 4th argument. – MCMXCII Jan 03 '18 at 17:41
  • Thank you for the right solution! Thank you!! – Joe Jan 03 '18 at 17:43
  • I suggest adding default values to your function itself, something like this: `function renderForm($date = '', $home = '', $time = '', $away = '', $city = '', $error = '')` - This will allow you to call `renderForm()` with no parameters to get a completely empty form. – GrumpyCrouton Jan 03 '18 at 17:56
  • [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](https://phpdelusions.net/pdo/mysqli_comparison) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Jan 03 '18 at 17:57
  • **Please**, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Jan 03 '18 at 17:57
  • Notice: Undefined index: date in /home/***/public_html/new.php on line 114 Notice: Undefined index: home in /home/***/public_html/new.php on line 116 Notice: Undefined index: time in /home/***/public_html/new.php on line 118 Notice: Undefined index: away in /home/***/public_html/new.php on line 120 Notice: Undefined index: city in /home/***/public_html/new.php on line 122 Is this why i am getting this error now? – Joe Jan 03 '18 at 18:07
  • did my answer help you? – Daniel H. Apr 05 '18 at 05:35

2 Answers2

0

renderForm('','',''); down the bottom of your code is missing a 4th argument.

MCMXCII
  • 1,043
  • 4
  • 13
  • 26
0

Your function is as follows:

function renderForm($date, $home, $time, $away, $city, $error){}

you require six total arguments to be passed to the function,

  • $date
  • $home
  • $time
  • $away
  • $city
  • $error

therefore you have to populate your function call with something for each of those

renderForm(null,null,null,null,null,null);

or you can use '' the empty string you show in your example. But something has to be passed for each argument.

Daniel H.
  • 422
  • 2
  • 15