Jump to content

Error adding to database via HTML form


eMonk

Recommended Posts

The php/mysql code below is called after a user fills out a html form and click on submit:

 

insert-model.php

 

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<h1>Model Entry Results</h1>

<?php
// create short variable names
$name=$_POST['name'];
$age=$_POST['age'];
$height=$_POST['height'];
$hair=$_POST['hair'];
$measurements=$_POST['measurements'];
$weight=$_POST['weight'];
$eyes=$_POST['eyes'];
$service=$_POST['service'];
$nationality=$_POST['nationality'];
$location=$_POST['location'];
$city_1=$_POST['city_1'];
$city_2=$_POST['city_2'];
$city_3=$_POST['city_3'];
$city_4=$_POST['city_4'];
$phone=$_POST['phone'];
$email_1=$_POST['email_1'];
$email_2=$_POST['email_2'];
$website=$_POST['website'];
$description=$_POST['description'];
$availability=$_POST['availability'];
$thumbnail=$_POST['thumbnail'];
$url=$_POST['url'];
$status=$_POST['status'];
$views=$_POST['views'];
$expiry_date=$_POST['expiry_date'];
$notes=$_POST['notes'];

if (!$name || !$thumbnail || !$url || !$views || !$expiry_date) {
  echo "You have not entered all the required details.<br />"
  ."Please go back and try again.";
  exit;
}

if (!get_magic_quotes_gpc()) {
  $name = addslashes($name);
  $height = addslashes($height);
  $hair = addslashes($hair);
  $measurements = addslashes($measurements);
  $eyes = addslashes($eyes);
  $nationality = addslashes($nationality);
  $location = addslashes($location);
  $phone = addslashes($phone);
  $email_1 = addslashes($email_1);
  $email_2 = addslashes($email_2);
  $website = addslashes($website);
  $description = addslashes($description);
  $availability = addslashes($availability);
  $thumbnail = addslashes($thumbnail);
  $url = addslashes($url);
  $expiry_date = addslashes($expiry_date);
  $notes = addslashes($notes);
}

@ $db = new mysqli('host', 'username', 'password', 'database'); // these values were removed

if (mysqli_connect_error()) {
  echo "Error: Could not connect to database. Please try again later.";
  exit;
}

$query = "insert into model values
  ('".$name."', '".$age."', '".$height."', '".$hair."', '".$measurements."', '".$weight."', '".$eyes."', '".$service."', '".$nationality."', '".$location."', '".$city_1."', '".$city_2."', '".$city_3."', '".$city_4."', '".$phone."', '".$email_1."', '".$email_2."', '".$website."', '".$description."', '".$availability."', '".$thumbnail."', '".$url."', '".$status."', '".$views."', '".$expiry_date."', '".$notes."')"; 
$result = $db->query($query);

if ($result) {
  echo $db->affected_rows." service provider inserted into the database.";
} else {
  echo "An error has occurred. The model was not added.";
}

$db->close();

?>
</body>
</html>

 

I keep getting the following error:

 

"An error has occurred. The model was not added."

 

Any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/234528-error-adding-to-database-via-html-form/
Share on other sites

I added in the following code but get the same error:

 

$query = "insert into model (model_name, age, height, hair, measurements, weight, eyes, service, nationality, location, city_1, city_2, city_3, city_4, phone, email_1, email_2, website, description, schedule, thumbnail, url, status, views, expiry_date, notes) values
  ('$name', '$age', '$height', '$hair', '$measurements', '$weight', '$eyes', '$service', '$nationality', '$location', '$city_1', '$city_2', '$city_3', '$city_4', '$phone', '$email_1', '$email_2', '$website', '$description', '$availability', '$thumbnail', '$url', '$status', '$views', '$expiry_date', '$notes')"; 
$result = $db->query($query);

 

$query = "insert into model (model_name, age, height, hair, measurements, weight, eyes, service, nationality, location, city_1, city_2, city_3, city_4, phone, email_1, email_2, website, description, schedule, thumbnail, url, status, views, expiry_date, notes) values

  ('$name', '$age', '$height', '$hair', '$measurements', '$weight', '$eyes', '$service', '$nationality', '$location', '$city_1', '$city_2', '$city_3', '$city_4', '$phone', '$email_1', '$email_2', '$website', '$description', '$availability', '$thumbnail', '$url', '$status', '$views', '$expiry_date', '$notes')";

 

after these, write these :

$result = mysql_result ( $query ) or die ( mysql_error() ) ;

 

now it will tell you what the problem is, copy it here if you can not figure it out.

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in /usr/www/virtual/user/domain/v1/admin/insert-model.php on line 75

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /usr/www/virtual/user/domain/v1/admin/insert-model.php on line 75

Access denied for user 'root'@'localhost' (using password: NO)

// create short variable names

$name=$_POST['name'];

$age=$_POST['age'];

$height=$_POST['height'];

$hair=$_POST['hair'];

$measurements=$_POST['measurements'];

$weight=$_POST['weight'];

$eyes=$_POST['eyes'];

$service=$_POST['service'];

$nationality=$_POST['nationality'];

$location=$_POST['location'];

$city_1=$_POST['city_1'];

$city_2=$_POST['city_2'];

$city_3=$_POST['city_3'];

$city_4=$_POST['city_4'];

$phone=$_POST['phone'];

$email_1=$_POST['email_1'];

$email_2=$_POST['email_2'];

$website=$_POST['website'];

$description=$_POST['description'];

$availability=$_POST['availability'];

$thumbnail=$_POST['thumbnail'];

$url=$_POST['url'];

$status=$_POST['status'];

$views=$_POST['views'];

$expiry_date=$_POST['expiry_date'];

$notes=$_POST['notes'];

 

echo $notes;

 

instead,

you just write

 

extract($_POST);

echo $notes;

 

Same thing.

Regarding security vulnerabilities and the use of extract:

 

Do not use extract() on untrusted data' date=' like user input[/color'] (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.