[RESOLU] Cannot access offset of type string

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : [RESOLU] Cannot access offset of type string

Re: Cannot access offset of type string

par nestor94 » 26 avr. 2023, 00:47

Merci pour les correctifs plus actuels !

Re: Cannot access offset of type string

par two3d » 25 avr. 2023, 19:02

Oui ça à l'air d'être bon.

Petit conseils : tu peux oublier la balise HTML font, obsolète, utilise le style CSS font-size à la place.

Tes echo, tu faire un raccourci avec un short code : <?= "Hello world" ?> à la place de <?php echo "Hello world"; ?>, plus sympa, non ? :D

Et donc pour ton image tu peut faire :

Code : Tout sélectionner

... <td> <a href="/achat_toto.php?id=<?= intval($resultat['id']) ?>"><img src="<?= htmlentities($resultat['produit']) ?>"></a> </td> ...

intval sur $resultat['id'] sert à rien, c'est avant l'insertion qu'il faut s’assurer d'avoir les bonnes valeurs ;)

Re: Cannot access offset of type string

par nestor94 » 25 avr. 2023, 18:40

J'ai enfin trouvé le soucis de concaténation à mon avis #-o
<table>
<?php
$sql = "SELECT * FROM toto";
$pdostat = $bdd->prepare($sql);
$pdostat->execute();
$resultat = $pdostat->fetch();    
$id = $resultat['id'];
$produit = $resultat['produit'];
$prix = $resultat['prix'];
$vendeur = $resultat['vendeur'];
$comment = $resultat['comment'];
?>
<table><font size="4">
<td>
<?php
echo '<a href="/achat_toto.php?id=' . intval($resultat['id']) . '"><img src="' . htmlentities($resultat['produit']) . '"></a>';
?>
</td>
<td><b><?php echo "$prix";?></b></td>
<td><b><?php echo"$vendeur";?></b></td>
<td><b><?php echo"$comment";?></b></td>
</table>

Re: Cannot access offset of type string

par or 1 » 25 avr. 2023, 00:29

Fais un var_dump() de tes variables pour comprendre ce qu'elles contiennent, ça t'aidera à débuguer ton code

Re: Cannot access offset of type string

par nestor94 » 25 avr. 2023, 00:01

<td><b><a href="achat_toto.php?produit=<br />
<b>Fatal error</b>:  Uncaught TypeError: Cannot access offset of type string on string in /home/clients/78f15c369c0786c4a419608e9e60028a/sites/xxxx.fr/achat_toto.php:102
Stack trace:
#0 {main}
  thrown in <b>/home/clients/78f15c369c0786c4a419608e9e60028a/sites/xxxx.fr/achat_toto.php</b> on line <b>102</b><br />

Re: Cannot access offset of type string

par or 1 » 24 avr. 2023, 17:48

quel est le code html généré par cette ligne de php ?

Re: Cannot access offset of type string

par nestor94 » 24 avr. 2023, 17:40

<td><b><a href="achat_toto.php?produit=<?php echo '<img src="' . htmlentities($produit['produit']) . '">';?>"</a></td>
En codant ainsi, je n'ai plus de message erreur mais rien ne s'affiche exceptées les lignes du tableau :(

Re: Cannot access offset of type string

par two3d » 24 avr. 2023, 14:03

Dans ton echo PHP tu mets des ', ce qui cause problème, tu doit concaténer ou échapper ton '

....php?produit='<img src="....

Re: Cannot access offset of type string

par nestor94 » 24 avr. 2023, 13:41

$sql = "SELECT * FROM toto ORDER BY produit DESC";
$pdostat = $bdd->prepare($sql);
$pdostat->execute();
$produit = 0;
while ($resultat = $pdostat->fetch())    
{
$produit++;
$produit = $resultat['produit'];
?>
<table><font size="4"><td><b><?php echo '<a href="/achat_toto.php?produit='<img src="' . htmlentities($produit['produit']) . '">'</a>';?></td>
J'ai modifié ainsi mais peut-être 1 problème concaténation

Parse error: syntax error, unexpected identifier "src", expecting "," or ";" in /home/clients/78f15c

Re: Cannot access offset of type string

par nestor94 » 24 avr. 2023, 13:38

Mes variables sont bien là et correctes

Re: Cannot access offset of type string

par @rthur » 24 avr. 2023, 11:52

Bonjour ?

Fais un var_dump() de tes variables pour comprendre ce qu'elles contiennent, ça t'aidera à débuguer ton code

Cannot access offset of type string

par nestor94 » 24 avr. 2023, 11:04

<?php
$sql = "SELECT * FROM toto ORDER BY produit DESC";
$pdostat = $bdd->prepare($sql);
$pdostat->execute();
$produit = 0;
while ($resultat = $pdostat->fetch())    
{
$produit++;
$id = $resultat['id'];
$produit = $resultat['produit'];
?>
<table><font size="5"><td>
<?php echo'<a href="/achat_toto.php?id=' . intval($id['id']) . '"><img src="' . htmlentities($produit['produit']) . '"></a>';?>
</table>
<?php
}
?>
le message d'erreur:
Cannot access offset of type string ............

Je n'arrive pas à trouver l'écriture correcte pour avoir un lien sur chaque produit listé.