Hi, im a total newbie and i’ve been struggling to understand PHP by reading someone’s else code that was not working (less than ideal but it is what it is).
I’ve made changes to the mysql database by creating new tables, updating names and trying to make everything run smooth. This is for a psychology experiment where subjects need to see an image, answer yes/no and rank from 0-100 the risk level.
I’m having issues in understanding why it’s not saving to the mysql database.
It’s giving “error Warning: mysqli_query() expects parameter 1 to be mysqli” error on lines
90,101 and 104
90 - if (mysqli_query($conn,$sql_user) === TRUE) {
101 - $result_user = mysqli_query($conn, $sql_user_query);
104 - while($row = mysqli_fetch_array($result_user))
Thanks in advance
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Estudio Trafico v2</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="mycss.css" rel="stylesheet">
<!-- Custom styles for this template -->
<style>
body {
padding-top: 54px;
}
@media (min-width: 992px) {
body {
padding-top: 56px;
}
}
/* Temporary navbar container fix */
.navbar-toggler {
z-index: 1;
}
@media (max-width: 576px) {
nav > .container {
width: 100%;
}
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarExample" aria-controls="navbarExample" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<p class="navbar-brand" style="margin-bottom: 2px;">Estudio sobre tráfico</p>
</div>
</div>
<!--
<div class="collapse navbar-collapse" id="navbarExample">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
-->
</div>
</nav>
<!-- Page Content -->
<div class="container">
<div id="intro" class="row">
<div class="col-lg-12 text-center">
<?php
if (!$conn) {
echo "error";
}
else{
$sql_user = "INSERT INTO users (`id`, `dni`, `gender`, `job`, `subjob`, `created_at`) VALUES (NULL, '`$id`, `$dni`, `$gender`, `$job`, `$subjob`, `$created_at`)";
}
if (mysqli_query($conn,$sql_user) === TRUE) { //ISSUE
$user_id = mysqli_insert_id($conn);
echo '<h3 class="mt-5">Datos almacenados con éxito!</h3>';
} else {
echo "Error:" . $sql_user . "<br>" . $conn-> error;
}
// Seleccionamos el usuario con esa id
$sql_user_query = "SELECT * FROM users WHERE id=$user_id";
$result_user = mysqli_query($conn, $sql_user_query); //ISSUE
// Únicamente habrá una fila (un usuario con esa id), damos valor a las variables
while($row = mysqli_fetch_array($result_user)) //ISSUE
{
$dni_query = $row['dni'];
$gender_query = $row['gender'];
$job_query = $row['job'];
$subjob_query = $row['subjob'];
$created_at_query = $row['created_at'];
}
?>
<?php
for($i = 1 ; $i <= $num_tasks ; $i++){
$task = $i;
$YES = "Sí";
$NO = "NO";
$img = $_POST["img".$i];
$time = $_POST["time".$i];
if(isset($_POST["confidence_level".$i])){
$confidence_level = $_POST["confidence_level".$i];
}
else{
$confidence_level = "";
}
if (!$conn) {
echo "error";
}
else{
$sql_data = "INSERT INTO data (`id`, `user_id`, `task`, `img`, `YES`, `NO`, `time`, `confidence_level`, `created_at`) VALUES (NULL, `$id`, `$user_id`, `$task`, `$img`, `$YES`, `$NO`, `$time`, `$confidence_level`, `$created_at`)";
}
if (mysqli_query($conn,$sql_data) === TRUE) {
$data_id = mysqli_insert_id($conn);
// Seleccionamos el dato con esa id
$sql_data_query = "SELECT * FROM data WHERE id=$data_id";
$result_data = mysqli_query($conn, $sql_data_query);
// Únicamente habrá una fila (un usuario con esa id), damos valor a las variables
while($row = mysqli_fetch_array($result_data))
{
$user_id_query = $row['user_id'];
$task_query = $row['task'];
$img_query = $row['img'];
$Sí_query = $row['YES'];
$NO_query = $row['NO'];
$time_query = $row['time'];
$confidence_level_query = $row['confidence_level'];
$created_at_query =$row['created_at'];
}
} else {
echo "Error:" . $sql_data . "<br>" . $conn-> error;
}
}
?>
</div>
<div class="col-lg-12 text-center" style="margin-top: 30px;">
<button style="margin-bottom: 40px" type="button" class="btn-primary selectable-element float-right" onclick="finish()">Finalizar</button>
</div>
</div>
</div>
</div>
<script src="myjs.js"></script>
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/tether/tether.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
</body>