QuiteNice QuiteNice - 5 days ago 5
HTML Question

PHP to mySQL Issue

Hello,

I'm trying to make this script work, without success.
I'w eliminated alot of errors but 1 problem is still there.

"Warning: mysqli_query() expects at least 2 parameters, 1 given in /mydomain.com/httpd.www/exampleForm.php on line 81"

I'm trying to create a simple PHP/HTML file so I can post to my mySQL table.

If someone finds better code, or changes outside the problem in this code please feel free to tell me :)

Thanks in advance for any help! And I'm sorry that I'm bad at English..

<?php

// Be sure to set up an SQL Table. The table is called newTable here.
// Fields required include formText, form, formDropDown, formRadio
// You should also add an autogenerated unique id as a primary key
// and a datestamp. See Tutorial #3 for more info

$link = mysqli_connect("@","@","@","@");
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);

if($_POST['formSubmit'] == "Submit")

// This code forces the user to fill out required fields.
// 1. Adapt this part of the form to make additional fields required.
// Required fields are optional.
// When adapting required fields, this part of the form needs one SQL designation updated.

{

$errorMessage = "";

if(empty($_POST['formText']))
{
$errorMessage .= "<li>Please enter some text.</li>";
}

// This block of code defines the variables
// 2. This is the first required area to update when you revise this form.
// When you add any fields to your form, you will need to define that field here.
// This part of the form requires two SQL designations updated (form and var).

$varText = $_POST['formText'];

//This is where the recipient of the e-mail is defined. Alter to your need.

$to = 'dev@olefm.no';

//This is where the subject of the e-mail is defined. Alter to your need.

$subject = "New Data Submitted";


// headers and subject
// this code should not be changed.

$headers = "MIME-Version: 1.0rn";

$headers .= "Content-type: text/html; charset=iso-8859-1rn";

// The e-mail that gets sent out will include this text in the body of the e-mail.
// You can use variables in this part.
// 3. If you have used variables in the body of the email
// you will need to update one field here.
// Variables in the e-mail are optional changes

$body = "Please see database for further details.

";
$body .= "Text entered: ".$varText."
" . " ";

mail($to, $subject, $body);

if(empty($errorMessage))

{

// This block of code inserts the data into the database using the variables we defined before.
// 4. This is the second required area to update when you revise this form.
// When you add any fields to your form, you will need to request that the data be inserted in this code.
// This part of the form requires two SQL designations updated (form and var).

$sql = "INSERT INTO lager (Varenavn) VALUES (".

PrepSQL($varText) . ", " .

mysqli_query($sql);

// The user is directed to this page after completing the form.
// This code assumes that the thankyou form is in the same folder as the example form.

#header("Location: thankYou.html");

exit();

}

}



// function: PrepSQL()
// Do not alter this code.
// It adds stripslashes and mysql_real_escape_string PHP functions
// This sanitizes the SQL query
// and inserts single quotes around the string

function PrepSQL($value)

{

// Stripslashes

if(get_magic_quotes_gpc())

{

$value = stripslashes($value);

}

}

?>

<!-- Begin HTML form -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>EXAMPLE FORM - PHP SQL</title>

<!-- define some style elements-->

<style>

label,a

{

font-family : Arial, Helvetica, sans-serif;

font-size : 12px;

}

.examplerule {
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
font-style: normal;
}
.examplestyle {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
</style>

</head>

<body>

<p>
<?php

if(!empty($errorMessage))

{

echo("<p>There was an error with your form:</p>\n");

echo("<ul>" . $errorMessage . "</ul>\n");

}

?>

<!-- 3. From here down is the form you see in Design View, and the page as it appears on the web. -->
<!-- Adapt this form by changing it to match what you are collecting in parts 1,2 and 4 -->

</p>
<p><strong><span class="examplestyle">Example Form
</span>
</strong>
</p>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<p>

<label for='formText'>Please enter some text. </label>
<br/>

<input type="text" name="formText" maxlength="50" value="<?=$varText;?>" />

</p>
</p>
</span>

<p>

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

</p>

</form>

</body>

</html>

Answer

You are using wrong syntax for mysqli_* functions.

For the specific warning that you mentioned, replace your code with the following:

mysqli_query($link, $sql); //$link is the connection you established with database.

You can refer this to get more details: http://php.net/manual/en/function.mysqli-connect.php

Comments