投稿フォームからこの 2 つの配列を正常に取得し、両方を print_r できるようになりました。今度はそれを挿入します。これは私が望んでいた順序ですが、データベースは更新されず、ファイルがアップロードされ、エラーは返されません。
PDO の何が問題になっていますか?
Adding_a_car.php
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
Year:<br>
<input type="text" name="year">
</li>
<li>
Make:<br>
<input type="text" name="make">
</li>
<li>
Model:<br>
<input type="text" name="model">
</li>
<li>
Engine:<br>
<input type="text" name="engine">
</li>
<li>
Sound System:<br>
<input type="text" name="sound_system">
</li>
<li>
Mileage:<br>
<input type="text" name="mileage">
</li>
<li>
Attribute 1:<br>
<input type="text" name="att1">
</li>
<li>
Attribute 2:<br>
<input type="text" name="att2">
</li>
<li>
Attribute 3:<br>
<input type="text" name="att3">
</li>
<li>
Attribute 4:<br>
<input type="text" name="att4">
</li>
<li>
Attribute 5:<br>
<input type="text" name="att5">
</li>
<li>
Attribute 6:<br>
<input type="text" name="att6">
</li>
<li>
Attribute 7:<br>
<input type="text" name="att7">
</li>
<li>
Attribute 8:<br>
<input type="text" name="att8">
</li>
<li>
Attribute 9:<br>
<input type="text" name="att9">
</li>
<li>
Attribute 10:<br>
<input type="text" name="att10">
</li>
<li>
Attribute 11:<br>
<input type="text" name="att11">
</li>
<li>
Attribute 12:<br>
<input type="text" name="att12">
</li>
<li>
Attribute 13:<br>
<input type="text" name="att13">
</li>
<li>
Attribute 14:<br>
<input type="text" name="att14">
</li>
<li>
Attribute 15:<br>
<input type="text" name="att15">
</li>
<li>Image1: <input name="image[]" type="file" /><br /></li>
<li>Image2: <input name="image[]" type="file" /><br /></li>
<li>Image3: <input name="image[]" type="file" /><br /></li>
<li>Image4: <input name="image[]" type="file" /><br /></li>
<li>Image5: <input name="image[]" type="file" /><br /></li>
<li>Image6: <input name="image[]" type="file" /><br /></li>
<li>Image7: <input name="image[]" type="file" /><br /></li>
<li>Image8: <input name="image[]" type="file" /><br /></li>
<li>Image9: <input name="image[]" type="file" /><br /></li>
<li>Image10: <input name="image[]" type="file" /><br /></li>
<li>Image11: <input name="image[]" type="file" /><br /></li>
<li>Image12: <input name="image[]" type="file" /><br /></li>
<li><input type="submit" value="Upload" /></li>
</ul>
</form>
<?php
if (isset($_POST))
{
$vehicleinfo = array
(
'year' => $_POST['year'],
'make' => $_POST['make'],
'model' => $_POST['model'],
'engine' => $_POST['engine'],
'sound_system' => $_POST['sound_system'],
'mileage' => $_POST['mileage'],
'att1' => $_POST['att1'],
'att2' => $_POST['att2'],
'att3' => $_POST['att3'],
'att4' => $_POST['att4'],
'att5' => $_POST['att5'],
'att6' => $_POST['att6'],
'att7' => $_POST['att7'],
'att8' => $_POST['att8'],
'att9' => $_POST['att9'],
'att10' => $_POST['att10'],
'att11' => $_POST['att11'],
'att12' => $_POST['att12'],
'att13' => $_POST['att13'],
'att14' => $_POST['att14'],
'att15' => $_POST['att15']
);
error_reporting(E_ALL);
$success = 0;
$picturepaths[12];
$ii = 0;
$fail = 0;
$pictureid = 1;
$i = 1;
$uploads_dir = 'images';
$count = rand(5, 70000);
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];
$name = $_FILES["image"]["name"][$key];
$uploadfile = "$uploads_dir/$name";
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext)){
$newfile = "$uploads_dir/picture".str_pad($count++,2,'0',STR_PAD_LEFT).".".$ext;
if(move_uploaded_file($tmp_name, $newfile)){
$success++;
$picturepaths[] = $newfile;
$ii++;
}else{
echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}else{
echo "Invalid Extension.\n";
$fail++;
}
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
//print_r($picturepaths);
//print_r($vehicleinfo);
add_a_car($picturepaths, $vehicleinfo);
関数 add_a_car();
function add_a_car($picturepaths, $vehicleinfo)
{
//$paths[12];
//$info[21];
$iii = 1;
$paths = array // here we import the picture unorganized and organize them
(
'picture1' => '',
'picture2' => '',
'picture3' => '',
'picture4' => '',
'picture5' => '',
'picture6' => '',
'picture7' => '',
'picture8' => '',
'picture9' => '',
'picture10' => '',
'picture11' => '',
'picture12' => ''
);
foreach($picturepaths as $field=>$data)
{
$paths['picture'.$iii] = '\''.$data.'\', ';
$iii++;
}
foreach($vehicleinfo as $field=>$data)
{
$info[] = '\''.$data.'\', ';
}
//i have both arrays working fully according and not missing info total of 33
$sql = "INSERT INTO auto(year, make, model, engine, sound_system, mileage, att1, att2,
att3, att4, att5, att6, att7, att8, att9, att10, att11, att12, att13, att14, att15, picture1,
picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9,
picture10, picture11, picture12) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
print_r($paths);
print_r($vehicleinfo);
$sth = $dbh->prepare($sql);
$sth->execute($final);
}
print_r($vehicleinfo) の結果 print_r($paths)
Notice: Undefined variable: info in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 5
Array ( [year] => 19154 [make] => cehedcad [model] => asdfasd [engine] => 454 [sound_system] => [mileage] => [att1] => [att2] => fas [att3] => [att4] => [att5] => [att6] => asdfa [att7] => sdfasdfas [att8] => [att9] => [att10] => [att11] => dfasdfa [att12] => dfas [att13] => [att14] => [att15] => ) Array ( [picture1] => 'images/picture26351.png', [picture2] => 'images/picture26352.png', [picture3] => 'images/picture26353.png', [picture4] => [picture5] => [picture6] => [picture7] => [picture8] => [picture9] => [picture10] => [picture11] => [picture12] => )
Notice: Undefined variable: dbh in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 48
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\cardealerwins\core\functions\users.php on line 48
これを試してみるとpdoがエラーを出してくれません
$dsn = 'mysql:dbname=cardealerwins;host=127.0.0.1';
$user = 'root';
$password = '';
$dbh = new PDO($dsn, $user, $password);
$sth = $dbh->prepare($sql);
$sth->execute($final);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
connection.php と pdo の新しいアップデート
<?php
$connect_error = 'sorry we\'re expierencing connection problems.';
mysql_connect('localhost', 'root', '') or die($connect_error);
mysql_select_db('cardealerwins') or die($connect_error);
$dsn = 'mysql:dbname=cardealerwins;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
$dbh->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$sth = $dbh->prepare($sql);
$sth->execute($final);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
print_r($sth->errorInfo());